#include <stdio.h>
#include <conio.h>
#include<string.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();
}
public class StringCommonCharInTwoString {
ReplyDeletepublic 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);
}
}