Searching string with strcspn(). - C String

C examples for String:String Function

Description

Searching string with strcspn().

Demo Code

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

int main( void )
{
    char  buf1[80], buf2[80];
    size_t loc;//  w  ww  .  ja  v  a  2s  .c  o  m

    printf("Enter the string to be searched: ");
    gets_s(buf1);
    printf("Enter the string containing target characters: ");
    gets_s(buf2);

    loc = strcspn(buf1, buf2);

    if ( loc ==  strlen(buf1) )
        printf("No match was found.");
    else
        printf("The first match was found at position %d.\n", loc);
    return 0;
}

Result


Related Tutorials