From cppreference.com
Defined in header
char*strstr(constchar*str,constchar*substr); (1) /*QChar*/*strstr(/*QChar*/*str,constchar*substr); (2) (since C23)1) Finds the first occurrence of the null-terminated byte string pointed to by substr in the null-terminated byte string pointed to by str. The terminating null characters are not compared.
2) Type-generic function equivalent to (1). Let T be an unqualified character object type.
If str is of type constT*, the return type is constchar*.
Otherwise, if str is of type T*, the return type is char*.
Otherwise, the behavior is undefined.
If a macro definition of each of these generic functions is suppressed to access an actual function (e.g. if (strstr) or a function pointer is used), the actual function declaration (1) becomes visible.
The behavior is undefined if either str or substr is not a pointer to a null-terminated byte string.
Parameters
str - pointer to the null-terminated byte string to examine substr - pointer to the null-terminated byte string to search for Return value
Pointer to the first character of the found substring in str, or a null pointer if such substring is not found. If substr points to an empty string, str is returned.
Example
Run this code
#include<stdio.h>#include<string.h>voidfind_str(charconst*str,charconst*substr){charconst*pos=strstr(str,substr);if(pos)printf("Found the string [%s] in [%s] at position %td\n",substr,str,pos-str);elseprintf("The string [%s] was not found in [%s]\n",substr,str);}intmain(void){charconst*str="one two three";find_str(str,"two");find_str(str,"");find_str(str,"nine");find_str(str,"n");return0;}Output:
Found the string [two] in [one two three] at position 4 Found the string [] in [one two three] at position 0 The string [nine] was not found in [one two three] Found the string [n] in [one two three] at position 1 References
C23 standard (ISO/IEC 9899:2024):
7.24.5.7 The strstr function (p: TBD)
C17 standard (ISO/IEC 9899:2018):
7.24.5.7 The strstr function (p: 269)
C11 standard (ISO/IEC 9899:2011):
7.24.5.7 The strstr function (p: 369)
C99 standard (ISO/IEC 9899:1999):
7.21.5.7 The strstr function (p: 332)
C89/C90 standard (ISO/IEC 9899:1990):
4.11.5.7 The strstr function
See also