codescracker


c

C Program to Check Strings are Anagram or Not



« Previous Program Next Program »


Check Anagram or Not in C

To check whether the two strings are anagram or not anagram in C programming, you have to ask to the user to enter the two string and start checking for anagram.

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

C Programming Code to Check Strings are Anagram or Not

Following is the C Anagram Program Source code:

/* C Program - Check Anagram or Not */
		
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
	clrscr();
	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("Strings are not Anagram to each other");
		}
		else
		{
			printf("Strings are Anagram");
		}
	}
	else
	{
		printf("Two string must have same number of character to be Anagram");
	}
	getch();
}

When the above c program is compile and executed, it will produce the following result. Above C Programming Example Output (Anagram String):

c anagram program

Above C Programming Example Output (Not Anagram String):

c program to check anagram or not

Here, fist we have to check the length of both the string, if length will be equal then further proceed else print the message for unequal length.

Now we compare first character of first string to all the character of second string one by one, then compare second character of first string to all the character of second string one by one, then compare third character of first string to all the character of second string one by one, and so on.

When comparing, if any of the character of the first string will match to the character of second string, then we initialize 1 to the found, and exit the loop and if there will no match found for any character, then 1 will not initialize to the found. So the found will be equal to 0, so that after exiting the inner loop, on checking "if(found==0)", it becomes true and 1 will be initialize to the variable not_found and exit the outer loop. Now after exiting or completing the 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 the strings are not anagram and if not_found will be equal to 0 then both the strings will be anagram

Same Program in Other Programming Language

You may also like same program in other programming languages:


« Previous Program Next Program »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test