Accept 2 String and display common chars in them.

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

void main()
{
char a[80],b[80];
int i,j,t;
clrscr();
printf("Enter 1st string\n");
gets(a);
fflush(stdin);
printf("Enter 2nd string\n");
gets(b);
printf("Common characters are :\n");
 for(i=0;a[i]!='\0';i++)
 {
  for(j=i-1;j>=0;j--)

   if(a[i]==a[j])
   break;
   if(j==-1)
   for(t=0;b[t]!='\0';t++)

     if(a[i]==b[t])
    {
     printf("%c",a[i]);
     break;
    }
  }

 getch();
}

1 comment:

  1. public class StringCommonCharInTwoString {
    public static void main(String[] args) {
    String s1 = "mahendra";
    String s2 = "rajendra";
    String commonChars = "";
    // Use the for loop to comapare the common char in String objects
    // as below code
    for (int i = 0; i < s1.length(); i++) {
    char chr = s1.charAt(i);
    if (s2.indexOf(chr) != -1) {
    commonChars = commonChars + chr;
    }
    }
    System.out.println("The common chars are : " + commonChars);
    }
    }

    ReplyDelete

Related Post

Array:
String functions:
General (Loops-for/while)
Write a Program to print reverse of a given number Write a Program to print Product of Digits (eg. 212=2*1*2=4) of a given number Write a C++ Program to Convert Binary into Decimal Number