C program to check if two strings are Anagram or not

Checking if two strings are anagram or not?

In this article we will learn how to code a C program to check if two strings are anagram or not. Two strings are said to be anagram if they contains same characters, only the order of characters is different. Therefore, in both strings, the frequency of each letter must be the same. For example, strings ‘listen’ and ‘silent’ are anagrams. We will count the frequencies of both the string and if the frequencies matches then strings entered are anagram.

C program to check if two strings are Anagram or not

Algorithm:

  • Initialize the variables.
  • Accept the inputs.
  • Calculate frequencies of both the strings.
  • If frequencies does not match set flag =1.
  • If flag= 0, string are anagram else they are not.
  • Print result.

C programming code to check if two strings are anagram or not

#include <stdio.h>
int main()
{
    //Initializing variables.
    char str1[100],str2[100];
    int first[26]={0}, second[26]={0}, c=0, flag=0;
    
    //Accepting inputs.
    printf("Enter First String: ");
    gets(str1);
    printf("Enter Second String: ");
    gets(str2);
    
    //Calculating frequencies of characters in first string.
    while(str1[c] != '\0')
    {
        first[str1[c]-'a']++;
        c++;
    }
     
    c=0;
    //Calculating frequencies of characters in second string. 
    while(str2[c] != '\0')
    {
        second[str2[c]-'a']++;
        c++;
    }
    //Checking if frequencies of both the strings are same or not.
    for(c=0;c<26;c++)
    {
        if(first[c] != second[c])
            flag=1;
    }
    //Priting result.
    if(flag == 0)
    {
        printf("\n%s and %s are Anagram Strings.",str1,str2);
    }
    else
    {
        printf("\n%s and %s are not Anagram Strings.",str1,str2);
    }
    return 0;
  
}
Output:
Enter First String: listen
Enter Second String: silent

listen and silent are Anagram Strings.

2 comments on “C program to check if two strings are Anagram or not”


    • HelpPrepInsta

      Hey Shreya, we are using this loop for checking the frequency of characters in the strings. Since there are 26 alphabets in English Language , we are iterating a loop from0 to 25 for checking their frequency