strstr
char * strstr ( const char * string1, const char * string2 ); | string.h |
cplusplus.com |
Find substring.
Scans string1 for the first occurrence of string2.
The search does not include terminating null-characters.
Parameters.
Return Value.
A pointer to the first occurrence of string2 in string1.
If string2 is not found in string1 the function returns NULL.
Portability.
Defined in ANSI-C.
ANSI-C++ standard specifies two different
declarations for this function instead of the one included in ANSI-C:
const char * strstr ( const char * string1, const char * string2 );
char * strstr ( char * string1, const char * string2 );
Both have the same behavior as the original declaration.
Example.
/* strstr example */
#include <stdio.h>
#include <string.h>
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 simple substring in str and substitutes that
word for sample.
Output:
This is a sample string
See also.
strcspn,
strspn,
strpbrk,
strchr