Name

strstr

Synopsis

Searches a string for a replica of another string

#include <string.h>
char *strstr( const char *s1, const char *s2 );

The strstr() function searches the string s1 for the first occurrence of the string s2 (not counting s2’s terminating null character). The return value is a pointer to the first character in the first occurrence in s1 of the sequence contained in s2, or a null pointer if there is no such occurrence. If s2 points to an empty string, then strstr() returns the value of its first argument, s1.

Example

FILE *fpTx, *fpRx, *fpLog;
char rxbuffer[1024], *found;
/* ... */
fgets( rxbuffer, 1024, fpRx );
found =strstr( rxbuffer, "assword:" );
if ( found != NULL )
{
  fputs( "Got password prompt. Sending password", fpLog );
  fputs( "topsecret", fpTx );
}

Get C in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.