Given two strings, print the count of intersection of characters in two strings
Input :
coderegister
programs
Output:
Input :
coderegister
programs
Output:
10 - count of uncommon characters in the both strings
Implementation:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
/* pointer to character array */
char *s,*v;
/* hash array of size 26 to hold alphabets count */
int hash[26],count=0,i;
/* initializing the hash array with a value 0 */
memset(hash,0,sizeof(hash));
/* allocate memory for the character arrays of 1000 bytes block &
return the address to character pointers *s & *v */
s=(char*)malloc(sizeof(char)*1000);
v=(char*)malloc(sizeof(char)*1000);
i=0;
/* get the two strings */
scanf("%s",s);
scanf("%s",v);
/* record the count of each of character in the main string */
while(*(s+i))
{
hash[*(s+i)-'a']++;
i++;
}
i=0;
/* for every same character occurrence in second string, decrease the value of hash index ,
Else increment the hash index */
while(*(v+i))
{
if(hash[*(v+i)-'a']!=0)
hash[*(v+i)-'a']--;
else
hash[*(v+i)-'a']++;
i++;
}
i=0;
/* count the remaining characters left in the array */
while(i<26)
{
count+=hash[i];
i++;
}
/* display the count */
printf("%d",count);
return 0;
}