How to write a C Program Anagrams in C Programming Language ?
Considering a string will only contain lower-case alphabets. It can also be done for uppercase letters , digits , special characters etc.
Solution:
- #include<stdio.h>
- #include<stdbool.h>
- #include<string.h>
- bool isAnagram(char *str1,char *str2)
- {
- int alphabetCount[26] = {0};
- int i = 0;
- if(strlen(str1) != strlen(str2))
- return false;
- for(i=0;str1[i]!='\0';++i)
- {
- ++alphabetCount[str1[i]-'a'];
- --alphabetCount[str2[i]-'a'];
- }
- for(i=0;i<26;++i)
- {
- if(alphabetCount[i])
- return false;
- }
- return true;
- }
- int main(void)
- {
- char string1[1001],string2[1001];
- /*
- considering a string will only contain lower-case alphabets
- It can also be done for uppercase letters , digits , special characters etc.
- */
- gets(string1);
- gets(string2);
- if(isAnagram(string1,string2))
- printf("yes");
- else
- printf("no");
- return 0;
- }