Searching string with strstr(). - C String

C examples for String:String Function

Description

Searching string with strstr().

Demo Code

#include <stdio.h>
#include <string.h>

int main( void )
{
    char *loc, buf1[80], buf2[80];

    printf("Enter the string to be searched: ");
    gets_s(buf1);/*from   w  w w.  jav  a2 s .c  o  m*/
    printf("Enter the target string: ");
    gets_s(buf2);

    /* Perform the search. */

    loc = strstr(buf1, buf2);

    if ( loc ==  NULL )
        printf("No match was found.\n");
    else
        printf("%s was found at position %d.\n", buf2, loc-buf1);
    return 0;
}

Result


Related Tutorials