strstr : source code
strstr : source code
(OP)
Hello and happy new year.
I am looking for strstr() code.
Every code I found had a bug!
could you help either find the bug or just post the correct one.
thanks a lot.
char *strstr2(char string1[], char string2[])
{// http://www.eskimo.com/~scs/cclass/notes/sx10d.html
char *start, *p1, *p2;
for(start = &string1[0]; *start != '\0'; start++)
{ /* for each position in input string... */
p1 = string2; /* prepare to check for pattern string there */
p2 = start;
while(*p1 != '\0')
{
if(*p1 != *p2) /* characters differ */
break;
p1++;
p2++;
}
if(*p1 == '\0') /* found match */
return start;
}
return NULL;
}
I am looking for strstr() code.
Every code I found had a bug!
could you help either find the bug or just post the correct one.
thanks a lot.
char *strstr2(char string1[], char string2[])
{// http://www.eskimo.com/~scs/cclass/notes/sx10d.html
char *start, *p1, *p2;
for(start = &string1[0]; *start != '\0'; start++)
{ /* for each position in input string... */
p1 = string2; /* prepare to check for pattern string there */
p2 = start;
while(*p1 != '\0')
{
if(*p1 != *p2) /* characters differ */
break;
p1++;
p2++;
}
if(*p1 == '\0') /* found match */
return start;
}
return NULL;
}
RE: strstr : source code
Please use the [code][/code] tags when posting code.
--
RE: strstr : source code
RE: strstr : source code
CODE
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);
}
RE: strstr : source code
RE: strstr : source code