Answer to Question #42159 in C++ for thabo Moletsane
Question #42159
A palindrome is a word or phrase that reads the same forwards and backwards, character for character,disregarding punctuation, case or spaces. Some examples are "racecar", "madam,I'm Adam".Write a program that allows the user to input a word or phrase and then determines if it is a palindrome.The program should use a Boolean Function procedure named Palindrome that returns the value True when the word or phrase is a palindrome and the value False otherwise
Expert's answer
//Answer on Question #42159 - Programming - C++
#include <algorithm>
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
bool notIsalnum(int c) {
return !::isalnum(c);
}
bool Palindrome(const string& phrase) {
string s = phrase;
s.erase(remove_if(s.begin(), s.end(), notIsalnum), s.end());
transform(s.begin(), s.end(), s.begin(), ::toupper);
return s == string(s.rbegin(), s.rend());
}
int main() {
cout << "Input word or phrase: " << endl;
string phrase;
getline(cin, phrase);
cout << "\"" << phrase << "\" is ";
if (!Palindrome(phrase)) cout << "not ";
cout << "a palindrome" << endl;
}
Need a fast expert's response?
Submit orderand get a quick answer at the best price
for any assignment or question with DETAILED EXPLANATIONS!
Comments
Leave a comment