Name

strstr, strcasestr, strnstr
- locate a substring

Library

libc.lib

Synopsis

  #include <string.h>
  char * strstr (const char *big, const char *little);
  char * strcasestr (const char *big, const char *little);
  char * strnstr (const char *big, const char *little, size_t len);

Return values

The strstr function returns a pointer to the beginning of the substring, or NULL if the substring is not found.

Detailed description

The strstr function locates the first occurrence of the null-terminated string little in the null-terminated string big.

The strcasestr function is similar to strstr, but ignores the case of both strings.

The strnstr function locates the first occurrence of the null-terminated string little in the string big, where not more than len characters are searched. Characters that appear after a \0’ character are not searched. Since the strnstr function is a
specific API, it should only be used when portability is not a concern.


Examples

#include <string.h>
#include <stdio.h>
int main()
{
    char *ptr;
    ptr = strstr("abcd", "z");
    if(ptr == NULL)
       printf("strstr: \"z\" not found in \"abcd\"\n");
    else
       printf("strstr: \"z\" found in \"abcd\"\n");
    ptr = strstr("abcd", "ab");
    if(ptr == NULL)
       printf("strstr: \"ab\" not found in \"abcd\"\n");
    else
       printf("strstr: \"ab\" found in \"abcd\"\n");
    ptr = strstr("abcd", "abcde");
    if(ptr == NULL)
       printf("strstr: \"abcde\" found in \"abcd\"\n");
    else
       printf("strstr: \"abbcde\" not found in \"abcd\"\n");
    return 0;
}

         

Output

strstr: "z" not found in "abcd"
strstr: "ab" found in "abcd"
strstr: "abcde" found in "abcd"

         

Return values

If little is an empty string, big is returned; if little occurs nowhere in big, NULL is returned; otherwise a pointer to the first character of the first occurrence of little is returned.

Examples

The following sets the pointer ptr to the "Bar Baz" portion of largestring:
const char *largestring = "Foo Bar Baz";
const char *smallstring = "Bar";
char *ptr;
ptr = strstr(largestring, smallstring);

         

The following sets the pointer ptr to NULL, because only the first 4 characters of largestring are searched:

const char *largestring = "Foo Bar Baz";
const char *smallstring = "Bar";
char *ptr;
ptr = strnstr(largestring, smallstring, 4);

         


See also

memchr, strchr, strcspn, strpbrk, strrchr, strsep, strspn, strtok
The strstr function conforms to -isoC.

Feedback

For additional information or queries on this page send feedback

© 2005-2007 Nokia

Top