The below program is to check whether two words are anagrams or not.The concept used is that we store the frequency of an alphabet in a word .By checking those counts we determine whether they are anagrams or not ....count same == anagrams.
#include <stdio.h>
#include<conio.h>
int anagramchecker(char [],char []); //declaration of funtion
int main()
{
char word1[50], word2[50];
printf("Enter first word:\n");
gets(word1);
printf("Enter second word:\n");
gets(word2);
anagramchecker(word1,word2); //calling function to check anagrams
getch(); return 0;
}
int anagramchecker(char a[], char b[])
{
int counta[26] = {0}, countb[26] = {0},i= 0;
while (a[i] != '\0')
{
counta[a[i]-'a']=counta[a[i]-'a']+1;
i++;
}
i= 0;
while (b[i] != '\0')
{
countb[b[i]-'a']= countb[b[i]-'a']+1;
i++;
}
for (i = 0; i< 26; i++)
{
if (counta[i] != countb[i])
{
printf("Given words are not anagrams.\n");
return 0;
}
}
printf("Given words are anagrams.\n");
return 1;
}
0 comments:
Post a Comment
Please Enter your comment here......