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.
Usage:
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:
#include<stdio.h>
#include<string.h>
int main ()
{
char a[] ="Just some random string";
char * ptr_b;
ptr_b = strstr (a,"string");
printf("%s\n",ptr_b);
ptr_b = strstr (a,"wrong");
printf("%s",ptr_b);
return 0;
}
Output of the example:
string
(null)
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.
You can leave a response, or trackback from your own site.
Tweet This! or use
to share this post with others.