C Reference String Operation: strstr()

The function strstr() returns a pointer to the first occurrence of x in a string.
It is also know as searching for a sub-string in a string.


char * strstr ( const char *, const char * );
char * strstr ( char * str1, const char * str2 );

Return Value

A pointer to the first occurrence in str1 of any of the entire sequence of characters specified in str2. If nothing is found a null pointer is returned if the sequence is not present in str1.

strstr() source code example:

int main ()
  char a[] ="Just some random string";
  char * ptr_b;
  ptr_b = strstr (a,"string");
  ptr_b = strstr (a,"wrong");
  return 0;

Output of the example:


Note: the first output the word string because the word is found, the second word isn’t found that’s why null is returned.

This entry was posted in C Reference string.h Functions. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed. Tweet This! Tweet This! or use to share this post with others.

Comments are closed.