Saturday 11 November 2017

C Program to Find the First Occurence of the any Character of String2 in String1


Code:

#include stdio.h>

void main()
{
    char s1[50], s2[10];
    int i, flag = 0;
    char *ptr1, *ptr2;

    printf("\nenter the string1:");
    scanf(" %[^\n]s", s1);    
    printf("\nenter the string2:");
    scanf(" %[^\n]s", s2);

    /*COMPARING THE STRING1 CHARACTER BY CHARACTER WITH ALL CHARACTERS OF STRING1*/
    for (i = 0, ptr1 = s1;*ptr1 !=  '\0';ptr1++)
    {
        i++;
        for (ptr2 = s2; *ptr2 != '\0';ptr2++)
        {
            if (*ptr1  ==  *ptr2)
            {
                flag = 1;
                break;
            }
        }
        if (flag  ==  1)
            break;
    }

    if (flag  ==  1)
        printf("\nfirst occurance of character of string2 in string1 is at position:%d and character is %c", i, *ptr2);
    else
        printf("\nnone of the characters of string1 match with mone of characters of string2");
}


Output:

enter the string1:C Programming Class

enter the string2:rnp

first occurance of character of string2 in string1 is at position:3 and character is p


More C Programs:












100+ Best Home Decoration Ideas For Christmas Day 2019 To Make Home Beautiful

Best gifts for Christmas Day | Greeting cards for Christmas Day | Gift your children a new gift on Christmas day This Christmas d...