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 string are anagram or not anagram in C++ programming, you have to ask to the user to enter the two string to start checking for anagram and display the result on the screen (whether the string is anagram or not) as shown here in the following program.

C++ Programming Code to Check Anagram or Not

Two string will be anagram to each other if and only if they contain the same number of characters, order of the 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:

Following C++ program shows how to check for anagram

C++ Programming Example Source Code:
/* C++ Program - Check Anagram or Not */
		
#include<iostream.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;
	cout<<"Enter first string :";
	gets(str1);
	cout<<"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)
		{
			cout<<"Strings are not Anagram to each other";
		}
		else
		{
			cout<<"Strings are Anagram";
		}
	}
	else
	{
		cout<<"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):

anagram program c++

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. Since to check for the anagram, the two string must having same 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, 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 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 strings are not anagram and if not_found will be equal to 0 then strings are anagram.

Same Program in Other Language

You may like the same program in other programming languages:


« Previous Program Next Program »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test