Wednesday, July 19, 2006

string functions in C (3)

strstr: scan str1, when first find occurrence of str2, pointer the poistion

char * __cdecl strstr (
const char * str1,
const char * str2
)
{
char *cp = (char *) str1;
char *s1, *s2;
if ( !*str2 )
return((char *)str1);
while (*cp)
{
s1 = cp;
s2 = (char *) str2;
while ( *s1 && *s2 && !(*s1-*s2) )
s1++, s2++;
if (!*s2)
return(cp);
cp++;
}

return(NULL);
}

No comments:

Post a Comment