codescracker
c

C Program to Check Strings are Anagram or Not



« Previous Program Next Program »

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:

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:

// Check Strings are Anagram are Not in C
// ----codescracker.com----

#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:

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


« Previous Program Next Program »