Please click on 1-2 Ads to keep this website's Study Material FREE and regularly Updated.

Program 10: Write a C++ program to check whether the two strings are anagram to each other or not.

#include<iostream>
#include<string.h>
using namespace std;

void anagram(string str1,string str2)
{
	if(str1.length()!=str2.length())
	{
		cout<<"\nThese strings are not anagram..";
		return;
	}

	for(int i=0;i<str1.length();i++)
	{
		int flag=0;
		for(int j=i;j<str2.length();j++)
		{
			if(str1[i]==str2[j])
			{
				flag=1;
				int temp=str2[i];
				str2[i]=str2[j];
				str2[j]=temp;
				break;
			}
		}
		if(flag==0)
		{
			cout<<"\nThese strings are not anagram..";
			return;
		}
	}
	cout<<"\nThese strings are anagram.";
}

int main()
{
	string str1,str2;
	cout<<"\nenter a string : ";
	cin>>str1;
	cout<<"\nenter it's anagram string : ";
	cin>>str2;
	anagram(str1,str2);
	return 0;
}



If you have any suggestion or found any error in the above program, then do let us know at [email protected] or just comment below.