Here is the C program to check whether two strings are anagram or not. Two strings are anagram of each other, if we can rearrange characters of one string to form another string.
All the characters of one string should appear same number of time in other string and their should not be any character which is only present in one string but not in other string. Strings can contain any ASCII characters.
For Example
"motherinlaw" and "womanhitler" are anagram.
"debit card" and "bad credit" are anagram.
C program to check if two strings are anagram by counting characters
Two strings are said to be anagram, if character frequency of both strings are identical. It means If all characters of one string appears same number of times in another string, then both strings are anagrams.
Algorithm for anagram check
- Length of both string must be same, otherwise they cannot be anagram.
- Count character frequency of first string.
- Count character frequency of second string.
- Compare character frequencies of both string. If same, then both strings are anagram otherwise not an anagram.
In this program, we are using a user defined function 'isAnagram' to check whether two strings are anagrams or not by implementing above mentioned algorithm. It returns 1, If both strings are anagram otherwise 0.
#include <stdio.h> #include <string.h> int isAnagram(char *firstString, char *secondString); int main(){ char firstString[100], secondArray[100]; printf("Enter first String \n"); gets(firstString); printf("Enter second String \n"); gets(secondArray); if(isAnagram(firstString, secondArray) == 1){ printf("%s and %s are Anagram\n",firstString,secondArray); } else { printf("%s and %s are not Anagram\n",firstString,secondArray); } return 0; } int isAnagram(char *firstString, char *secondString){ int firstCharCounter[256] = {0}, secondCharCounter[256] = {0}; int counter; if(strlen(firstString) != strlen(secondString)){ return 0; } for(counter = 0; firstString[counter] != '\0'; counter++){ firstCharCounter[firstString[counter]]++; } for(counter = 0; secondString[counter] != '\0'; counter++){ secondCharCounter[secondString[counter]]++; } for(counter = 0; counter < 256; counter++){ if(firstCharCounter[counter] != secondCharCounter[counter]) return 0; } return 1; }Output
Enter first String TECH CRASH COURSE Enter second String RASHC THEC URSEOC TECH CRASH COURSE and RASHC THEC URSEOC are Anagram
Enter first String Apple Enter second String Banana Apple and Banana are not Anagram
C program to check if two strings are anagram by sorting characters of strings
If two strings are anagram, then both strings will become same after sorting the characters of both string.
For Example
apple and pelap are anagram, after sorting
apple becomee aelpp
and pelap also becomes aelpp
- Length of both string must be same, otherwise they cannot be anagram.
- Sort characters of both strings.
- If after sorting, both strings becomes identical then anagram otherwise not an anagram.
#include <stdio.h> #include <string.h> int isAnagram(char *firstString, char *secondString); void sortString(char* inputString); int main(){ char firstString[100], secondArray[100]; printf("Enter first String \n"); gets(firstString); printf("Enter second String \n"); gets(secondArray); if(isAnagram(firstString, secondArray) == 1){ printf("%s and %s are Anagram\n", firstString, secondArray ); } else { printf("%s and %s are not Anagram\n", firstString, secondArray ); } return 0; } int isAnagram(char *firstString, char *secondString){ if(strlen(firstString) != strlen(secondString)){ return 0; } sortString(firstString); sortString(secondString); if(strcmp(firstString, secondString) == 0){ return 1; } else { return 0; } } void sortString(char* inputString){ int counterArray[256] ={0}, length, counter, index; length = strlen(inputString); for(counter = 0; counter < length; counter++){ counterArray[inputString[counter]]++; } for(counter=0,index=0;counter<256;counter++){ if(counterArray[counter] != 0){ while(counterArray[counter] > 0){ inputString[index++] = counter; counterArray[counter]--; } } } inputString[index] = '\0'; }Output
Enter first String apple Enter second String pelap Both strings are Anagram
Related Topics