×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

strstr : source code
3

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;

    }

RE: strstr : source code

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

Please use the [code][/code] tags when posting code.

--

RE: strstr : source 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);

}

RE: strstr : source code

You'd think MS would know better than to post-increment when they don't need to use the temporary variable that's created...

RE: strstr : source code

(OP)
thanks a lot.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close