C Program to check whether two Strings are Anagram or not

Before going to the program first let us understand what is Anagram?

Anagram:

          An Anagram is the result of rearranging the letters of a word or phrase to produce a new word or phrase, using all the original letters exactly once.

Example: Let us consider two Strings as given below:

“adda” and “dada” 

In the above Strings the letter of “adda” can be rearranged to form dada”.

Thus adda and dada are Anagram Strings.

Now let us see the program code to check whether two Strings are Anagram or not and understand the code using the Explanation given below.

Program code to check whether two Strings are Anagram or not:

#include<stdio.h>
#include<conio.h>

void main()
{
    char s1[100],s2[100];
    int a[26]={0}, b[26]={0}, c=0, flag=0;
    clrscr();
    
    printf("\n Enter First String: ");
    gets(s1);
    
    printf("\n Enter Second String: ");
    gets(s2);
    
    while(s1[c] != '\0')
    {
        a[s1[c]-'a']++;
        c++;
    }
    
    c=0;
    
    while(s2[c] != '\0')
    {
        b[s2[c]-'a']++;
        c++;
    }
    
    for(c=0;c<26;c++)
    {
        if(a[c] != b[c])
            flag=1;
    }
    
    if(flag == 0)
    {
        printf("\n %s and %s are Anagram Strings.",s1,s2);
    }
    else
    {
        printf("\n %s and %s are not Anagram Strings.",s1,s2);
    }
    getch();
}

Explanation:

  • First the computer reads the two strings and stores it in the “s1” and “s2” variables respectively using the following lines:
printf("\n Enter First String: ");
gets(s1);
    
printf("\n Enter Second String: ");
gets(s2);
  • Then using the while loop it reads character by character of the Strings and stores the count of each letters in the “a” and “b” array variables respectively using the following lines:
while(s1[c] != '\0')
{
    a[s1[c]-'a']++;
    c++;
}
   
c=0;
   
while(s2[c] != '\0')
{
    b[s2[c]-'a']++;
    c++;
}

Note: ‘\0’ is the NULL character used to represent the End the String.

  • Then using for loop and if condition the “a” and “b” arrays are compared and if not equal then “flag” is set to “1”  using the following lines:
for(c=0;c<26;c++)
{
    if(a[c] != b[c])
        flag=1;
}
  • Finally if-else condition is used to print the Strings are Anagram or not on the screen using the following lines:
if(flag == 0)
{
    printf("\n %s and %s are Anagram Strings.",s1,s2);
}
else
{
    printf("\n %s and %s are not Anagram Strings.",s1,s2);
}

Step by Step working of the above Program Code:

  1. Let us assume that a user enters the two Strings as “dada” and “adda”.
  2. So it stores the Strings in s1[]=dada and s2[]=adda and also assigns c=0 and flag=0.

Step for reading character by character of the Strings and stores the count of each letters in the “a” array variable:

  1. Then the loop continues till the condition of while loop is true.

3.1.   s1[0] != ‘\0’   (d’!=’\0′)    while loop condition is true

a[s1[0]-‘a’]++    (a[‘d’-‘a’]++)    So,  a[3]=a[3]+1

So,  a[3]=1

c++    (c=c+1)    So,  c=1

3.2.   s1[1] != ‘\0’   (a’!=’\0′)    while loop condition is true

a[s1[1]-‘a’]++    (a[‘a’-‘a’]++)    So,  a[0]=a[0]+1

So,  a[0]=1

c++    (c=c+1)    So,  c=2

3.3.   s1[2] != ‘\0’   (‘d’!=’\0′)    while loop condition is true

a[s1[2]-‘a’]++    (a[‘d’-‘a’]++)    So,  a[3]=a[3]+1

So,  a[3]=2

c++    (c=c+1)    So,  c=3

3.4.   s1[3] != ‘\0’   (‘a’!=’\0′)    while loop condition is true

a[s1[3]-‘a’]++    (a[‘a’-‘a’]++)    So,  a[0]=a[0]+1

So,  a[0]=2

c++    (c=c+1)    So,  c=4

3.5.   s1[4] != ‘\0’   (‘\0′!=’\0’)    while loop condition is false

It comes out of the while loop and assigns c=0.

Step for reading character by character of the Strings and stores the count of each letters in the “b” array variable:

  1.  Then the loop continues till the condition of while loop is true.

4.1.   s2[0] != ‘\0’   (‘a‘!=’\0’)    while loop condition is true

b[s2[0]-‘a’]++    (b[‘a’-‘a’]++)    So,  b[0]=b[0]+1

So,  b[0]=1

c++    (c=c+1)    So,  c=1

4.2.   s2[1] != ‘\0’   (‘d‘!=’\0’)    while loop condition is true

b[s2[1]-‘a’]++    (b[‘d’-‘a’]++)    So,  b[3]=b[3]+1

So,  b[3]=1

c++    (c=c+1)    So,  c=2

4.3.   s2[2] != ‘\0’   (‘d’!=’\0′)    while loop condition is true

b[s2[2]-‘a’]++    (b[‘d’-‘a’]++)    So,  b[3]=b[3]+1

So,  b[3]=2

c++    (c=c+1)    So,  c=3

4.4.   s2[3] != ‘\0’   (‘a’!=’\0′)    while loop condition is true

b[s2[3]-‘a’]++    (b[‘a’-‘a’]++)    So,  b[0]=b[0]+1

So,  b[0]=2

c++    (c=c+1)    So,  c=4

4.5.   s2[4] != ‘\0’   (‘\0′!=’\0’)    while loop condition is false

It comes out of the while loop.

Step for comparing “a” and “b” arrays:

  1. Then it assigns the value of c=0 and the loop continues till the condition of the for loop is true.

5.1.   c<26    (0<26)    for loop condition is true

a[0] != b[0]   (2!=2)    if condition is false

  c++    (c=0+1)     So  c=1

5.2.   c<26    (1<26)    for loop condition is true

a[1] != b[1]   (0!=0)    if condition is false

  c++    (c=1+1)     So  c=2

5.3.  c<26    (2<26)    for loop condition is true

a[2] != b[2]   (0!=0)    if condition is false

 c++    (c=2+1)     So  c=3

5.4.  c<26    (3<26)    for loop condition is true

a[3] != b[3]   (2!=2)    if condition is false

 c++    (c=3+1)     So  c=4

5.5.  It continues till c<26 and comes out of the for loop when c=26

Note: The value of “flag=0” remains as it is because the “if condition is false” every time for the above comparisons.

Step for printing the Strings are Anagram or not:

  1. Finally using if-else condition it prints Anagram or not.

6.1.   flag==0    (0==0)    if condition is true

So it prints dada and adda are Anagram Strings.

Note: If the if condition is false,

then, it prints <s1> and <s2> are not Anagram Strings.

where s1, s2 are the Strings entered by user.

  1. Thus program execution is completed.

Output:

anagram

anagram1

TO DOWNLOAD THE PROGRAM CODE : CLICK HERE

 

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *