Strstr
char* strstr(char *str1, char *str2)
{
    char *s1, *s2, *s3;
    for (s1 = str1; *s1 != '\0'; s1++) {
        s3 = s1;
        for (s2 = str2; *s2 != '\0' && *s3 != '\0'; s2++, s3++)
            if (*s2 != *s3)    // if not equal, break
                break;
        if (*s3 == '\0')    // if *s3 reach '\0', no match
            return NULL;
        else if (*s2 == '\0')   // if *s2 == '\0', then all are equal 
            return s1;     // break, and s1 point to matched point
    }
    return NULL;
}
 
int main()
{
    char str1[] ="This is a sample string.";
    char str2[] ="sam";
    char *pch;
    pch = strstr(str1, str2);
    printf("%s@\n", pch);
}

Output:

sample string.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License