cplusplus
cplusplus

 

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 and right.
  • 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:

  1. Copy the code and save it to a file with the extension .cpp (e.g., palindrome_checker.cpp).
  2. Open a C++ compiler or IDE (like Code::Blocks, Visual Studio, or g++).
  3. Compile the program using the command g++ palindrome_checker.cpp -o palindrome_checker in the terminal or use the build option in your IDE.
  4. Run the program by executing the command ./palindrome_checker in the terminal or using the run option in your IDE.
  5. 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.

 

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 :)