Check If Two Strings Are Anagram using C++
Solving a real-world problem in a digital way only requires the ability to be able to code. Well for some programming is tough while for some it’s the most interesting thing to do. However, things get a bit complicated when problems get difficult. Therefore practising tricky questions gives our brain a quick start to make it faster. Especially when you do programming using C++. And for a fact, C++ is considered one of the hardest programming languages to learn. But that doesn’t mean you can’t master it. In this article we will try to solve a simple C++ program, that is we are going to check if two strings are anagram or not.
Practising these types of questions also helps to get an upper edge in Competitive Programming. So open up your IDE and let’s get started real quick. Once you understand the logic practice this program on your own to make your brain work in a problem-solving way. This is a somewhat easy question so you might not get asked in an interview. But make sure to practice it on your own after reading this article.
What is An Anagram String?
- An anagram of a string is a string that consists same no of characters representing the same letters as that of the string we’re comparing with. Though anagram strings have the same character count the order in which characters are written should be different in both the strings.
- For example, “abcd” and “dcba” are an anagram of each other.
What’s The Approach?
- We will create two strings that we are going to compare in this program. So we will simply use the sorting method to check if the input strings are anagrams or not.
- However, before that, we will check the length of both the strings if the length is not similar then it is clear that strings are not anagrams.
- However, if the length of strings is equal then we will sort both of them in ascending order using the
sort()
function. After that, we will check if both the sorted strings are equal to each other or not. If yes then strings are anagrams and vice versa.
Also Read: How To Run C++ in Visual Studio Code on Mac OS
C++ Program To Check If Two Strings Are Anagram
Input:
str1
=
"LISTEN"
str2
=
"SILENT"
The two strings are anagram of each other
// C++ program to check whether two strings are anagrams of each other #include <bits/stdc++.h> using namespace std; /* function to check whether two strings are anagram of each other */ bool areAnagram(string str1, string str2) { // Get lengths of both strings int n1 = str1.length(); int n2 = str2.length(); // If length of both strings is not same, then they // cannot be anagram if (n1 != n2) return false; // Sort both the strings sort(str1.begin(), str1.end()); sort(str2.begin(), str2.end()); // Compare sorted strings for (int i = 0; i < n1; i++) if (str1[i] != str2[i]) return false; return true; } // Driver code int main() { string str1 = "LISTEN"; string str2 = "SILENT"; // Function Call if (areAnagram(str1, str2)) cout << "The two strings are anagram of each other"; else cout << "The two strings are not anagram of each " "other"; return 0; }