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

«
»

This is a C Program to find the first occurence of the any character of String2 in string1 & also its position.

Problem Description

This program takes two strings as input and finds the first occurence of the any character of String2 in string1 & also its position.

Problem Solution

1. Take two strings as input.
2. Compare both the strings using two pointers.
3. Print the character which matches first and its position.

Program/Source Code

Here is source code of the C Program to find the first occurence of the any character of String2 in string1 & also its position. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2. /*
  3.  * C Program to Find the First Occurence of the any Character of 
  4.  * String2 in string1 & also its Position  
  5.  */
  6. #include <stdio.h>
  7.  
  8. void main()
  9. {
  10.     char s1[50], s2[10];
  11.     int i, flag = 0;
  12.     char *ptr1, *ptr2;
  13.  
  14.     printf("\nenter the string1:");
  15.     scanf(" %[^\n]s", s1);    
  16.     printf("\nenter the string2:");
  17.     scanf(" %[^\n]s", s2);
  18.  
  19.     /*COMPARING THE STRING1 CHARACTER BY CHARACTER WITH ALL CHARACTERS OF STRING1*/
  20.     for (i = 0, ptr1 = s1;*ptr1 !=  '\0';ptr1++)
  21.     {
  22.         i++;
  23.         for (ptr2 = s2; *ptr2 != '\0';ptr2++)
  24.         {
  25.             if (*ptr1  ==  *ptr2)
  26.             {
  27.                 flag = 1;
  28.                 break;
  29.             }
  30.         }
  31.         if (flag  ==  1)
  32.             break;
  33.     }
  34.  
  35.     if (flag  ==  1)
  36.         printf("\nfirst occurance of character of string2 in string1 is at position:%d and character is %c", i, *ptr2);
  37.     else
  38.         printf("\nnone of the characters of string1 match with mone of characters of string2");
  39. }
Program Explanation

1. Take two strings as input and store it in the array’s s1[] and s2[].
2. Use pointers ptr1 and ptr2 to point the array’s s1[] and s2[] respectively.
3. Compare each character of the array s2[] with the array s1. Use the variable i to find the position.
4. Print the character which matches first and the variable i for the position.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement
Runtime Test Cases
 
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

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at programming examples on all topics, go to C Programming Examples.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.