Home >Backend Development >C++ >Usage of strstr function in c language
The strstr() function searches a string for a substring, returning a pointer to the first matching substring or NULL (not found). The usage steps are as follows: 1. Search from the beginning of the string; 2. Compare character by character to find a match or reach the end of the string; 3. Return the matching character pointer if found, or NULL if not found.
Usage of strstr function in C language
The strstr function is used to find subcharacters in a string Standard C function for strings. It returns a pointer to the first matching substring, or NULL if no match is found.
Syntax:
<code class="c">char *strstr(const char *haystack, const char *needle);</code>
Parameters:
Return value:
Usage:
The strstr function works as follows:
Example:
<code class="c">#include <stdio.h> #include <string.h> int main() { char haystack[] = "Hello, world!"; char needle[] = "world"; char *result = strstr(haystack, needle); if (result) { printf("Found '%s' at position %ld\n", needle, result - haystack); } else { printf("'%s' not found in '%s'\n", needle, haystack); } return 0; }</code>
Output:
<code>Found 'world' at position 7</code>
The above is the detailed content of Usage of strstr function in c language. For more information, please follow other related articles on the PHP Chinese website!