По заданию я должен разработать функцию, которая проверяет является ли строка палиндромом или нет. Строка может содержать пунктуацию и пробелы. Например: “Madam,I’m Adam”.По-сути я пытаюсь упростить данную строку до “madamimadam”.
Вот мой код:
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
bool isPal(const string &);
int main()
{
string input;
cout << "Enter a string to check if this is a palindrome: ";
getline(cin, input);
if (isPal(input))
cout << "Yes, this is the palindrome." << endl;
else
cout << "No, it's not." << endl;
return 0;
}
bool isPal(const string & st)
{
string temp = st;
temp.erase(remove_if(temp.begin(), temp.end(), isspace), temp.end());
temp.erase(remove_if(temp.begin(), temp.end(), ispunct), temp.end());
if ( temp == string(temp.rbegin(), temp.rend()))
return true;
else
return false;
}
Простые слова типа otto функция выдает правильно, но с более сложными строками не получается. Подскажите, пожалуйста, что не так.