Difference between revisions of "c/string/byte/strstr"
From cppreference.com
m (→References: +C17ref; fmt) |
(standardized position of pointer asterisks) |
||
Line 4: | Line 4: | ||
{{dcl header | string.h}} | {{dcl header | string.h}} | ||
{{dcl | | {{dcl | | ||
− | char *strstr( const char* str, const char* substr ); | + | char *strstr( const char *str, const char *substr ); |
}} | }} | ||
{{dcl end}} | {{dcl end}} | ||
Line 28: | Line 28: | ||
#include <stdio.h> | #include <stdio.h> | ||
− | void find_str(char const* str, char const* substr) | + | void find_str(char const *str, char const *substr) |
{ | { | ||
− | char* pos = strstr(str, substr); | + | char *pos = strstr(str, substr); |
pos ? printf("found the string '%s' in '%s' at position %td\n", | pos ? printf("found the string '%s' in '%s' at position %td\n", | ||
substr, str, pos - str) | substr, str, pos - str) | ||
Line 39: | Line 39: | ||
int main(void) | int main(void) | ||
{ | { | ||
− | char* str = "one two three"; | + | char *str = "one two three"; |
find_str(str, "two"); | find_str(str, "two"); | ||
find_str(str, ""); | find_str(str, ""); |
Revision as of 08:53, 11 December 2022
Defined in header <string.h>
|
||
char *strstr( const char *str, const char *substr ); |
||
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.
The behavior is undefined if either str
or substr
is not a pointer to a null-terminated byte string.
Contents |
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 <string.h> #include <stdio.h> void find_str(char const *str, char const *substr) { char *pos = strstr(str, substr); pos ? printf("found the string '%s' in '%s' at position %td\n", substr, str, pos - str) : printf("the string '%s' was not found in '%s'\n", substr, str); } int main(void) { char *str = "one two three"; find_str(str, "two"); find_str(str, ""); find_str(str, "nine"); find_str(str, "n"); return 0; }
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
- 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
finds the first occurrence of a character (function) | |
finds the last occurrence of a character (function) | |
C++ documentation for strstr
|