Read lines until the input ends and report whether each is a palindrome, ignoring case and anything that is not a letter or digit.
#include <cctype>
#include <iostream>
#include <string>
bool is_palindrome(const std::string& s) {
std::string clean;
for (unsigned char c : s)
if (std::isalnum(c)) clean += static_cast<char>(std::tolower(c));
return clean == std::string(clean.rbegin(), clean.rend());
}
int main() {
std::string line;
while (std::getline(std::cin, line))
std::cout << '"' << line << "\" " << (is_palindrome(line) ? "is" : "is not") << " a palindrome\n";
return 0;
}
"Racecar" is a palindrome "Level up" is not a palindrome "Was it a car or a cat I saw" is a palindrome
Normalise first – keep letters and digits in lower case – then compare with the reversed copy. Iterating as unsigned char keeps the ctype calls safe.