C Program to Check Strings are Anagram or Not

In this tutorial, we will learn about how to create a program in C that checks whether any given two string are Anagram or not

Check Anagram or Not in C

To check whether any given two strings (by user at run-time) are anagram or not in C programming, you have to ask from user to enter the two string to check and find out that both Strings are Anagram or not as shown in the program given below.

Two string will be anagram to each other, if and only if they contains same number of characters, order of characters doesn't matter. That is, if two strings are anagram to each other, then one string can be rearranged to form the other string. For Example:

  • abc and cba are anagram.
  • creative and reactive are anagram
  • course and source are anagram

The two string say super and upper are not Anagram. Both strings contains same number of character but doesn't contains each and every character of super in upper (or upper in super), that is you can not rearrange the string super to form upper (or upper to form super).

C Programming Code to Check Strings are Anagram or Not

Let's take a look at the program to check whether the given two strings are Anagram or not in C. Here is the C Anagram Program Source code:

#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
    char str1[20], str2[20];
    int len, len1, len2, i, j, found=0, not_found=0;
    printf("Enter first string: ");
    gets(str1);
    printf("Enter second string: ");
    gets(str2);
    len1 = strlen(str1);
    len2 = strlen(str2);
    if(len1 == len2)
    {
        len = len1;
        for(i=0; i<len; i++)
        {
            found = 0;
            for(j=0; j<len; j++)
            {
                if(str1[i] == str2[j])
                {
                    found = 1;
                    break;
                }
            }
            if(found == 0)
            {
                not_found = 1;
                break;
            }
        }
        if(not_found == 1)
            printf("\nStrings are not Anagram");
        else
            printf("\nStrings are Anagram");
    }
    else
        printf("\nBoth string must contain same number of character to be an Anagram Strings");
    getch();
    return 0;
}

As the above program was written under Code::Blocks IDE, therefore after successful build and run, you will get the following output. Here is the initial snapshot of the sample run:

c anagram program

Now supply any two string say social and soliat as input and press ENTER key to see the output as shown in the second snapshot of the sample run given here:

c program to check anagram or not

Let's take another sample run and check for another two string say super and upper. Here is the final snapshot of the sample run:

c check anagram or not

As you can see from the above output where the two string say super and upper has supplied. The first string say super has s as extra character and one p as missing character. And the second string say upper has second p as extra character and one character that is s is missing from it. Therefore we can not rearrange super to form upper (or upper to form super).

Let's check for another two string say creative and reactive using following sample run. Here is the final snapshot of the sample run:

c check string anagram not

Here are some of the main steps used in above program:

  • Receive any two string as input
  • Use strlen() function to find length of both the string
  • That is initialize the length of first string to any variable say len1 and the length of second string to any variable say len2
  • Now check whether length of both strings are equal or not
  • If both strings doesn't have same number of character, then print any message like Both strings must have to contain same number of character to be an Anagram String
  • And if both strings contains same number of character, then follow the steps given below to check whether each and every character of first string is present inside the second string or not
  • Create a for loop that runs from 0 to one less than the string's length (either first or second because we have checked and found that both strings contains same number of character)
  • Inside the for loop, initialize 0 to any variable say found that increments its value by 1 if a character from first string gets found in second string
  • Create another for loop that also runs from 0 to one less than the string length
  • Here we have created two for loops in a way to compare each and every character of first string to each and every character of second string
  • While comparing, if character from first string gets matched to the character of second string, then initialize 1 to found variable and break out from the inner for loop using break keyword
  • After running the inner for loop, check whether found holds its original value (0) or not
  • If it holds, then the current character of first string does not found in second string. Therefore we have checked after each and every complete run of inner for loop that whether found holds its original value or not. If it holds (that is 0), then we have to initialize 1 to not_found variable. And get out from the outer for loop. As if any of the character from first string does not gets found in second string, it means we have not to check for next remaining character as the string can not be an Anagram string to each other if any character from first string does not gets matched with character in second string
  • At last we have checked, whether the variable say not_found holds its original value 0 or not
  • If it holds, then both strings are Anagram. Otherwise both strings are not Anagram (in case it holds the value as 1)

The brief description of the above program is, fist we have to check length of both the string, if length will be equal then proceed further, otherwise print the message for unequal length. We have compared the first character of first string to all the character of second string one by one, then compared the second character of first string to all the character of second string one by one, then compared the third character of first string to all the character of second string one by one, and so on.

While comparing, if any of the character of the first string gets match to the character of second string, then we have initialized 1 to the found, and exit from inner loop. If there will no match found for any character, then 1 will not be initialized to the found. So the found holds its previous or initial value that is 0.

And after exiting the inner loop, on checking "if(found==0)", it becomes true and 1 will be initialized to the variable not_found and exit from the outer loop. Now after exiting from outer loop, we have to check whether not_found will be equal to 1 or 0. If not_found will be equal to 1, then both strings are not anagram and if not_found will be equal to 0 then both strings will be anagram.

Same Program in Other Language

C Online Test


« Previous Program Next Program »



Like/Share Us on Facebook 😋