Question
C++ Question: Write and demonstrate a function that checks to see iftwo strings are anagrams . For this exercise, youshould ignore spaces as part of
C++ Question:
Write and demonstrate a function that checks to see iftwo strings are anagrams. For this exercise, youshould ignore spaces as part of the calculation.Example: 'i am lord voldemort' is an anagram of 'tom marvoloriddle'.
At the moment, I'm having trouble ignoring spaces.
My code is this:
bool isAnagram() {
string str1;
string str2;
cout << "String 1: ";
getline(cin, str1);
cout << "String 2: ";
getline(cin, str2);
int n1 = str1.length();
int n2 = str2.length();
if (n1 != n2) {
return false;
}
sort(str1.begin(), str1.end());
sort(str2.begin(), str2.end());
for (int i = 0; i < n1; i++) {
if (str1[i] != str2[i]) {
// cout << "Not anagram.";
return false;
}
}
// cout << "Anagram";
return true;
}
int main() {
if (isAnagram())
cout << "The two strings are anagram of each other";
else
cout << "The two strings are not anagram of each "
"other";
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started