Format
#include <string.h> char *strstr(const char *string1, const char *string2);
Language Level: ANSI, POSIX, XPG4
strstr finds the first occurrence of string2 in string1.
The function ignores the null character (\0) that ends string2
in the matching process.
Return Value
strstr returns a pointer to the beginning of the first
occurrence of string2 in string1. If string2
does not appear in string1, strstr returns NULL. If string2
points to a string with zero length, strstr returns string1.
Example
This example locates the string haystack in the string
"needle in a haystack".
#include <string.h> #include <stdio.h>
int main(void)
{
char *string1 = "needle in a haystack";
char *string2 = "haystack";
char *result;
result = strstr(string1,string2);
/* Result = a pointer to "haystack" */
printf("%s\n", result);
return 0;
/****************************************
The output should be:
haystack ****************************************/ }
![]()
strchr -- Search for Character
strcmp -- Compare Strings
strcspn -- Compare Strings for Substrings
strncmp -- Compare Strings
strpbrk -- Find Characters in String
strrchr -- Find Last Occurrence of
Character in String
strspn -- Search Strings
wcschr -- Search for Wide Character
wcscspn -- Find Offset of First
Wide-Character Match
wcspbrk -- Locate Wide Characters in
String
wcsrchr -- Locate Wide Character in
String
wcsspn -- Search Wide-Character
Strings
wcswcs -- Locate Wide-Character
Substring
<string.h>