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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!