Count of common characters in two strings ID:210


Two string values S1 and S2 are passed as input. The program must print the count of common characters in the strings S1 and S2.

Input Format:
First line will contain the value of string S1
Second line will contain the value of string S2

Output Format:
First line will contain the count of common characters.

Boundary Conditions:
Length of S1 and S2 is from 3 to 100.

Sample Input/Output:

Example 1:
Input:
china
india

Output:
3

Explanation:
The common characters are i,n,a


Example 2:
Input:
energy
every

Output:
4

Explanation:
The common characters are e,e,r,y

PROGRAM IN C:

#include<stdio.h>

#include<stdlib.h>

#define MAX 256

 

int *CountArray(char *str)

{

 int *count=(int *)calloc(sizeof(int),MAX);

 int i; 


 for(i=0;*(str+i);i++)

 {

  count[*(str+i)]++;

 }

 return count;

}

 

int CountCommonChar(char *str1, char *str2)

{

 int *count=CountArray(str1);

 int i,count_char=0;

 for(i=0;*(str2+i);i++)

 {

  if(count[*(str2+i)]!=0)

  {

   count_char++;

   count[*(str2+i)]--;

  }

 }

 

 return count_char;

}

int main()

{

 char str1[100],str2[100];

 

 scanf("%s",str1);

 scanf("%s",str2);

 

 int n=CountCommonChar(str1,str2);

 printf("%d",n);

 

 return 0;

}




Comments

Popular Posts