const char * strstr ( const char * str1, const char * str2 );
char * strstr ( char * str1, const char * str2 );
locate substring
returns a pointer to the first occurrence of str2 in str1, or a null pointer if there str2 is not part of str1.
the matching process does not include the terminating null-characters.
parameters
str1
c string to be scanned.
str2
c string containing the sequence of characters to match.
return value
a pointer to the first occurrence in str1 of any of the entire sequence of characters specified in str2, or a null pointer if the sequence is not present in str1.
portability
in c, this function is declared as:
char * strstr ( const char *, const char * );
instead of the two overloaded versions provided in c++.
example
/* strstr example */
#include <stdio.h>
#include <string.h>
int main ()
{
char str[] ="this is a simple string";
char * pch;
pch = strstr (str,"simple");
strncpy (pch,"sample",5);
puts (str);
return 0;
}
this example searches for the "simple" substring in str and replaces that word for "sample".
output:
this is a sample string