ALTEN Interview Question

Write a CPP program on palindrome numbers

Interview Answer

Anonymous

May 24, 2024

#include #include #include // For std::transform bool isPalindrome(const std::string &str) { int left = 0; int right = str.length() - 1; while (left < right) { if (str[left] != str[right]) { return false; } left++; right--; } return true; } int main() { std::string input; std::cout << "Enter a string: "; std::getline(std::cin, input); // Convert the string to lowercase to make the palindrome check case-insensitive std::transform(input.begin(), input.end(), input.begin(), ::tolower); // Remove non-alphanumeric characters (optional, uncomment if needed) /* input.erase(std::remove_if(input.begin(), input.end(), [](char c) { return !std::isalnum(c); }), input.end()); */ if (isPalindrome(input)) { std::cout << "The string is a palindrome." << std::endl; } else { std::cout << "The string is not a palindrome." << std::endl; } return 0; }