Reputation: 49
So far my program prompts the user to enter a string and then read from start to end and backwards and compares both results. Obviously, if they are equal it's a palindrome, if not - it ain't.
#include <iostream>
#include <stdio.h>
using namespace std;
int main(){
string input;
cout << "Enter a string: ";
cin >> input;
if (input == string(input.rbegin(), input.rend())) {
cout << "Yes";
} else {
cout << "No";
}
return 0;
}
Question is, how do I omit things like whitespaces, punctuation etc so that I can use sentences and check them if are a palindrome or not?
Upvotes: 1
Views: 1399
Reputation:
Just process the input first:
#include <algorithm>
#include <boost/algorithm/string/case_conv.hpp>
#include <cctype>
input.erase(
std::remove_if(input.begin(), input.end(),
[] (char c) { return std::isspace(c) || std::ispunct(c); }),
input.end()
);
boost::algorithm::to_lower(input);
Upvotes: 4