Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Smith and Roberson Business Law

Authors: Richard A. Mann, Barry S. Roberts

15th Edition

1285141903, 1285141903, 9781285141909, 978-0538473637

More Books

Students also viewed these Programming questions