C Program to Check If Two Strings are Anagram

  • Write a C program to check whether two strings are anagram or not.
  • Algorithm to check anagram string.

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.

/*
* C Program to check whether two given strings are anagram 
* by counting frequency of characters
*/
#include <stdio.h>
#include <conio.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);
    }
    getch();
    return 0;
}

/*
 * Function to check whether two passed strings are anagram or not
 * returns 1 if anagram otherwise 0
 */
int isAnagram(char *firstString, char *secondString){
    int firstCharCounter[256] = {0}, secondCharCounter[256] = {0};
    int counter;
    /* Two Strings cannot be anagram if their length is not equal */
    if(strlen(firstString) != strlen(secondString)){
        return 0;
    }
    /* count frequency of characters of firstString */
    for(counter = 0; firstString[counter] != '\0'; counter++){
        firstCharCounter[firstString[counter]]++;
    }
    /* count frequency of characters of secondString */
    for(counter = 0; secondString[counter] != '\0'; counter++){
        secondCharCounter[secondString[counter]]++;
    }
    /* compare character counts of both strings, 
       If not equal return 0, otherwise 1 */
    for(counter = 0; counter < 256; counter++){
        if(firstCharCounter[counter] != secondCharCounter[counter])
            return 0;
    }
    return 1;
}
Program 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.
/*
* C Program to check whether two given strings 
* are anagram or not using sorting
*/
#include <stdio.h>
#include <conio.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 );
    }
    getch();
    return 0;
}

/*
 * Function to check whether two passed strings are anagram or not
 * returns 1 if anagram otherwise 0
 */
int isAnagram(char *firstString, char *secondString){
    /* Two Strings cannot be anagram if their length is not equal */
    if(strlen(firstString) != strlen(secondString)){
        return 0;
    }
    /* Sort both strings and check if they are equal */
    sortString(firstString);
    sortString(secondString);
    if(strcmp(firstString, secondString) == 0){
        /* Both input strings are anagram */
        return 1;
    } else {
        return 0;
    }
}

/*
 * Function to sort characters of a string 
 */
void sortString(char* inputString){
    /* initialize counterArray to 0 */
    int counterArray[256] ={0}, length, counter, index;
    length = strlen(inputString);
    /* Count frequency of characters in input array*/
    for(counter = 0; counter < length; counter++){
        counterArray[inputString[counter]]++;
    }
    /* Populate output array */
    for(counter = 0, index = 0; counter < 256; counter++){
        if(counterArray[counter] != 0){
            while(counterArray[counter] > 0){
                inputString[index++] = counter;
                counterArray[counter]--;
            }
        }
    }
    inputString[index] = '\0';
}
Program 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