Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

strstr : source code 3

Status
Not open for further replies.

CCNProjects

Technical User
Nov 4, 2005
64
CA
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[])
{//
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;

}
 
What bug (as in, it seems to work for me)?

Please use the [tt][ignore]
Code:
[/ignore][/tt]
tags when posting code.

--
 
MS VC++ 6.0 RTL source:
Code:
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);

}
 
You'd think MS would know better than to post-increment when they don't need to use the temporary variable that's created...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top