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:

  1. Take input from the user as a string.
  2. Remove any non-alphanumeric characters and convert the string to lowercase to ensure the check is case-insensitive and ignores punctuation.
  3. 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 to cleanedStr.
    • 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.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)