C Program to Check If Two Strings are Anagram

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
  1. Length of both string must be same, otherwise they cannot be anagram.
  2. Count character frequency of first string.
  3. Count character frequency of second string.
  4. 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

Algorithm for anagram check by sorting characters of strings
  • 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
C program to find a substring from a given string
C program to remove extra spaces from string
C program to swap two strings
C Program to find frequency of characters in a string
C program to convert lowercase string to uppercase
C program to check string is palindrome
C program to remove vowels from a string
List of all C programs