Palindrome Checker in C++
A palindrome is a string that reads the same forward and backward. In this program, we will check if a given string is a palindrome using C++.
Program Explanation
The program works as follows:
- Take input from the user as a string.
- Remove any non-alphanumeric characters and convert the string to lowercase to ensure the check is case-insensitive and ignores punctuation.
- Compare the cleaned string with its reverse to check if it’s a palindrome.
C++ Code
// Palindrome Checker in C++
#include <iostream>
#include <algorithm>
#include <cctype>
#include <string>
using namespace std;
/**
* @brief Function to check if a given string is a palindrome.
* @param str The input string to check.
* @return true if the string is a palindrome, false otherwise.
*/
bool isPalindrome(const string& str) {
string cleanedStr;
// Remove non-alphanumeric characters and convert to lowercase
for (char ch : str) {
if (isalnum(ch)) {
cleanedStr += tolower(ch);
}
}
// Compare cleaned string with its reverse
string reversedStr = cleanedStr;
reverse(reversedStr.begin(), reversedStr.end());
return cleanedStr == reversedStr;
}
int main() {
string input;
cout << "Enter a string: ";
getline(cin, input);
if (isPalindrome(input)) {
cout << "The string is a palindrome." << endl;
} else {
cout << "The string is not a palindrome." << endl;
}
return 0;
}
Explanation of the Code
- Include Headers: The program includes necessary headers for input/output, string manipulation, and algorithms.
#include <iostream> #include <algorithm> #include <cctype> #include <string>
- Namespace: Using the standard namespace for convenience.
using namespace std;
- isPalindrome Function: This function checks if a given string is a palindrome.
bool isPalindrome(const string& str) { string cleanedStr; // Remove non-alphanumeric characters and convert to lowercase for (char ch : str) { if (isalnum(ch)) { cleanedStr += tolower(ch); } } // Compare cleaned string with its reverse string reversedStr = cleanedStr; reverse(reversedStr.begin(), reversedStr.end()); return cleanedStr == reversedStr; }
This function takes the following steps:
- Iterates over each character of the input string.
- Checks if the character is alphanumeric using
isalnum
. - Converts the character to lowercase using
tolower
and appends it tocleanedStr
. - Reverses
cleanedStr
and compares it with the original cleaned string.
- main Function: This is the entry point of the program.
int main() { string input; cout << "Enter a string: "; getline(cin, input); if (isPalindrome(input)) { cout << "The string is a palindrome." << endl; } else { cout << "The string is not a palindrome." << endl; } return 0; }
The
main
function:- Prompts the user to enter a string.
- Calls the
isPalindrome
function to check if the string is a palindrome. - Outputs the result to the console.
Sample Output
Here is an example of the program in action:
Enter a string: A man, a plan, a canal, Panama
The string is a palindrome.
Enter a string: Hello, World!
The string is not a palindrome.