This short tutorial will show you how to use C’s strstr() function to locate a set substring.
The syntax for the strstr() function is:
The function accepts two arguments: str and the substring. The function then searches for the occurrence of the substr in the string pointed by the str.
NOTE: The function does not include the terminating null characters but stops the search at their first occurrence.
If it finds the occurrence of the substring, the function returns a pointer to the first character of the substring. If it does not find the searched substring, the function returns a null pointer.
Strstr() Function Example
The following simple example illustrates how the function works.
If we run the code above, we should get an output shown:
That is it for the strstr() function in C. Check out other C tutorials to learn more.