Introduction:
A palindrome is a word, phrase, number, or other sequence of characters that reads the same forwards and backwards (ignoring spaces, punctuation, and capitalization).
In this program, we will write a C++ function to check whether a given string is a palindrome.
This is a common problem used to test basic string manipulation and understanding of algorithms.
Objective:
The objective of this program is to check if a given string is a palindrome. The program will take user input, process the string,
and determine whether it is a palindrome by comparing the characters from both ends of the string.
Code:
#include #include #include using namespace std; // Function to check if a given string is a palindrome bool isPalindrome(string str) { // Convert the string to lowercase for case-insensitivity transform(str.begin(), str.end(), str.begin(), ::tolower); int left = 0; int right = str.length() - 1; // Loop to compare characters from both ends while (left < right) { // Skip non-alphanumeric characters if (!isalnum(str[left])) { left++; } else if (!isalnum(str[right])) { right--; } else { // Compare characters if (str[left] != str[right]) { return false; } left++; right--; } } return true; } int main() { string str; // Take user input cout << "Enter a string: "; getline(cin, str); // Check if the string is a palindrome if (isPalindrome(str)) { cout << "The string is a palindrome." << endl; } else { cout << "The string is not a palindrome." << endl; } return 0; }
Explanation:
1. Header Files: The program includes the necessary header files: <iostream>
for input and output, <string>
for string manipulation, and <algorithm>
for functions like transform
.
2. Function isPalindrome: This function takes a string as input and checks if it is a palindrome.
- The string is first converted to lowercase to make the comparison case-insensitive.
- Non-alphanumeric characters (spaces, punctuation) are ignored using the
isalnum
function. - We then compare the characters from both ends of the string using two pointers,
left
andright
. - If at any point the characters don’t match, the function returns
false
indicating the string is not a palindrome. - If all the character pairs match, the function returns
true
.
3. Main Function: The main
function:
- Prompts the user to enter a string.
- Calls the
isPalindrome
function with the user’s input. - Outputs whether the string is a palindrome based on the result of the function.
How to Run the Program:
- Copy the code and save it to a file with the extension .cpp (e.g.,
palindrome_checker.cpp
). - Open a C++ compiler or IDE (like Code::Blocks, Visual Studio, or g++).
- Compile the program using the command
g++ palindrome_checker.cpp -o palindrome_checker
in the terminal or use the build option in your IDE. - Run the program by executing the command
./palindrome_checker
in the terminal or using the run option in your IDE. - Enter a string when prompted, and the program will output whether the string is a palindrome.
Example:
Enter a string: A man a plan a canal Panama The string is a palindrome.
In this example, the program checks the phrase “A man a plan a canal Panama” and correctly identifies it as a palindrome by ignoring spaces and capitalization.