The strstr() function in C is used to parse and locate the occurrence of a substring in a string. It is defined in the string.h header file.
This short tutorial will show you how to use C’s strstr() function to locate a set substring.
Basic Usage
The syntax for the strstr() function is:
char* strstr(char *str, const char *substr);
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.
Return Value
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:
Substring located!
LinuxHint Team
Conclusion
That is it for the strstr() function in C. Check out other C tutorials to learn more.
About the author
<img alt="" data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/john-150×150.png612658f41742b.jpg" height="112" src="data:image/svg xml,” width=”112″>
John Otieno
My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list