Updated July 7, 2023
Introduction to Anagram in C++
The algorithm we have developed is called anagram to find the number of characters in the given two strings and to compare each character in the same strings. Write a function to see whether or not two strings are anagrams. The string anagram has the same characters, and the order can only differ. To understand how anagram works, you can see the example of an anagram that is “TRIANGLE “ and “INTEGRAL,” “SILENT,” and “LISTEN” are the anagrams of each other.
Examples of Anagram in C++
Below are examples to implement:
Example #1
By sorting
Code:
// C++ program to see if two strings are mutually anagrams
#include <bits/stdc++.h>
using namespace std;
/* function to check whether two strings are each anagrams */
bool areAnagram(string abc1, string abc2)
{
// Get both strings lengths
int n1 = abc1.length();
int n2 = abc2.length();
// If both strings are not equal in length, they are not anagram
if (n1 != n2)
return false;
// Filter the strings of both
sort(abc1.begin(), abc1.end());
sort(abc2.begin(), abc2.end());
for (int i = 0; i < n1; i++)
if (abc1[i] != abc2[i])
return false;
return true;
}
// Driver code
int main()
{
string abc1 = "hello";
string abc2 = "olleh";
if (areAnagram(abc1, abc2))
cout << "This two string are anagram to with each other";
else
cout << "This two strings are not anagram to with each other";
return 0;
}
Output:
Example #2
Program in C++ to search if the two strings are an anagram of each other or not.
Code:
#include <bits/stdc++.h>
using namespace std;
#define NO_OF_CHARS 256
/* function to test whether two strings are each anagram */
bool areAnagram(char* abc1, char* abc2)
{
// Build 2 count arrays and start all values with 0.
int count1[NO_OF_CHARS] = { 0 };
int count2[NO_OF_CHARS] = { 0 };
int i;
// Raise number in the respective count array for each character in the input strings
for (i = 0; abc1[i] && abc2[i]; i++) {
count1[abc1[i]]++;
count2[abc2[i]]++;
}
// If there is a different length of both strings. Removal would cause the software to fail for strings like
// "aaca" and "aca"
if (abc1[i] || abc2[i])
return false;
for (i = 0; i < NO_OF_CHARS; i++)
if (count1[i] != count2[i])
return false;
return true;
}
/* Driver code*/
int main()
{
char abc1[] = "educba";
char abc2[] = "abcuda";
if (areAnagram(abc1, abc2))
cout << "The two strings are one anagram";
else
cout << "The two strings are not one anagram";
return 0;
}
Output:
Example #3
By using the hash map method. Program to check whether strings are anagrams or not by using the hash map method.
Code:
#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;
bool isValidAnagramString(string abc, string cbe) {
if(abc.length() != cbe.length()) return false;
if(abc.empty() && cbe.empty()) return true;
unordered_map<char, int> myMap;
for(char c : abc) {
myMap[c] += 1;
} // End the for loop
// Read the string cbe and check it in myMap
for(char c : cbe) {
myMap[c] -= 1;
} // End the for loop
for(auto it = myMap.begin(); it != myMap.end(); ++it ) {
if (it->second != 0) {
return false;
}
}
return true;
}
int main() {
string a1, a2;
cout << "Enter the two strings names: ";
cin>>a1>>a2;
if(isValidAnagramString(a1, a2)) {
cout << "Valid Anagram name String" << endl;
} else {
cout << "Not Anagram name String" << endl;
}
return 0;
}
Output:
Explanation of the above program: In this case, first, the length of each string must be tested; if the string length is equal, then the message should be printed on unequal length. The two strings must be the same length since the anagram is verified. We first compare the first string character with all the second-string characters one by one, then compare the second string character with the other string character one by one, and then compare the first string character with all the other string characters, one by one, and so on.
Conclusion
In this article, we have seen how to check whether two strings are an anagram of each other or not by using various methods and examples. I hope you will find this article helpful.
Recommended Articles
This is a guide to Anagram in C++. Here we discuss the basic concept and examples of an anagram in C++ with proper codes and outputs. You can also go through our other related articles to learn more –