Here is the program for anagram in c.
Two strings are said to be anagrams if by rearranging the characters of any one string will make both strings equal.
Example:
“adssfa” and “dsasfa” are anagrams
“adsfa” and “sdfac” are not anagrams
“adsfa” and “sdfac” are not anagrams
How to Check two Strings are Anagrams or not?
So what we will do is find the frequency of each characters in first and second string and store it in two arrays. Now we will check the frequency of each character in two strings by comparing the two arrays. If every character has same frequency then the strings are anagrams otherwise not. Below I have written a C program to implement this logic. If you are finding any difficulty then comment below, I will try to solve your problem.
Anagram Program in C
#include<stdio.h> #include<string.h> #include<stdlib.h> //function to count the frequency of each character void count_frequency(char str[],int s[]) { int i=0,j,count; while(str[i]!='\0') { j=0; count=0; while(str[j]!='\0') { if(str[i]==str[j]) count++; j++; } s[str[i]-97]=count; i++; } } int main() { char str1[100],str2[100]; int i,j,flag=1,s1[26]={0},s2[26]={0}; printf("Enter first string:"); scanf("%s",str1); printf("Enter second string:"); scanf("%s",str2); if(strlen(str1)!=strlen(str2)) //if the lengths of two strings are not equal { printf("\nStrings are not anagrams"); exit(0); } count_frequency(str1,s1); count_frequency(str2,s2); for(i=0;i<26;++i) //checking freuency of each character { if(s1[i]!=s2[i]) { flag=0; break; } } if(flag) printf("\nStrings are anagrams"); else printf("\nStrings are not anagrams"); return 0; }
Output:
Can you please explain me what is the meaning of using s[str[i]-97]=count; and
for(i=0;i<26;++i)
{
if(s1[i]!=s2[i])
{
flag=0;
break;
}
}
in above program…………
I also had same doubt
can you explain what this does?
s[str[i]-97]=count;
in the code
how is the frequency stored I am not able to get it.