14.7. strstr()

Find a string in another string.

Prototypes

#include <string.h>
char *strstr(const char *str, const char *substr);

Description

Let's say you have a big long string, and you want to find a word, or whatever substring strikes your fancy, inside the first string. Then strstr() is for you! It'll return a pointer to the substr within the str!

Return Value

You get back a pointer to the occurance of the substr inside the str, or NULL if the substring can't be found.

Example

char *str = "The quick brown fox jumped over the lazy dogs.";
char *p;

p = strstr(str, "lazy");
printf("%s\n", p); // "lazy dogs."

// p is NULL after this, since the string "wombat" isn't in str:
p = strstr(str, "wombat");

See Also

strchr(), strrchr(), strspn(), strcspn()