Jump to: navigation, search

Description

The strstr() function locates the first occurrence in the string pointed to by s1 of the sequence of characters (excluding the terminating null character) in the string pointed to by s2.

Return value

The strstr function returns a pointer to the located string, or a null pointer if the string is not found. If s2 points to a string with zero length, the function returns s1.

Prototype

Declared in string.h

The C89/C99 prototype is:

char *strstr(const char *s1, const char *s2);

Implementation

In standard C, this can be implemented as:

#include <string.h> /* size_t memcmp() strlen() */
char *strstr(const char *s1, const char *s2)
{
    size_t n = strlen(s2);
    while(*s1)
        if(!memcmp(s1++,s2,n))
            return s1-1;
    return 0;
}

Compilable unit, portable C90 in implementation namespace; public domain; past reviewers: none; current reviews: none

References

The C Standard, 7.21.5.7 (C99 numbering)

Personal tools