C++ Program to Check Whether a String is a Palindrome

Palindrome string

C++ programs to check whether a string is a palindrome have been shown here. A string is called a palindrome if the same string is obtained when the string is reversed. For example, the string "madam" is a palindrome as we get the same string "madam" while reading from left to right or right to left. But the string "cat" is not a palindrome as we get "tac" after reading the string from right to left.

Here both of the iterative and recursive approaches have been covered. Another approach of string comparison using inbuilt functions like strcmp() and strrev() have also been shown. The algorithm, pseudocode and time complexity of the program have been provided below.






1. Algorithm to check whether a string is a palindrome


1. Take a string $str$ and the size $n$ as inputs.

2. Check for each $i$ from 0 to $n/2 - 1$, if $str[i] = str[n - 1 - i]$.

3. If all the comparisions return true, declare $str$ as a palindrome.

4. Else declare $str$ as not a palindrome.





2. Pseudocode to check whether a string is a palindrome


Input: A string $str$ and size of string $n$

Output: $str$ is a palindrome or not

1. Procedure checkPalindrome($str$, $n$):

2. Repeat for $i \in [0, n / 2)$:

3. If $str[i] \neq str[n - 1- i] $

4. Return "not palindrome"

5. Return "palindrome"

6. End Procedure





3. Time complexity to check whether a string is a palindrome


Time Complexity: O(n)

Here $n$ is the length of the string.





4. Program & output to check whether a string is a palindrome




4.1. C++ Program & output to check whether a string is a palindrome using Iteration

Code has been copied
/******************************************
            alphabetacoder.com
C++ program to check whether a string is a 
palindrome or not using iteration
*******************************************/

#include <iostream>
#include <cstring>

using namespace std;

int main() {
    // declare character array to store strings
    char str[30];
    // declare variables
    int i, length, flag = 0;

    // take input of string
    cout << "Enter the string: ";
    cin >> str;

    // calculate the length of string
    length = strlen(str);

    // divide the array in two parts
    // compare charcaters of the left part to the right part
    // traverse left part from left to right direction
    // traverse right part from right to left direction
    for (i = 0; i < (length / 2); i++) {
        // check for character mismatch 
        if (str[i] != str[length - 1 - i]) {
            cout << str << " is not a palindrome string!" << endl;
            flag = 1;
            break;
        }
    }

    // if flag = 0, the string is palindrome 
    if (flag == 0)
        cout << str << " is a palindrome string!" << endl;

    return 0;
}

Output


Case 1:

Enter the string: level

level is a palindrome string!


Case 2:

Enter the string: program

program is not a palindrome string!




4.2. C++ Program to check whether a string is a palindrome using strrev( ) and strcmp( ) functions

Code has been copied
/******************************************
           alphabetacoder.com
C++ program to check whether a string is a 
palindrome or not using inbuilt functions
*******************************************/

#include <iostream>
#include <cstring>

using namespace std;

int main() {
    // declare character arrays to store strings
    char str[30], rev[30];

    // take input of string
    cout << "Enter the string: ";
    cin >> str;

    // copy the string str to rev
    strcpy(rev, str);

    // reverse the string rev using strrev() 
    strrev(rev);

    // compare strings str and rev using strcmp()
    // check if both str and rev are same
    if (strcmp(str, rev) == 0)
        cout << str << " is a palindrome string!" << endl;
    else
        cout << str << " is not a palindrome string!" << endl;

    return 0;
}



4.3. C++ Program to check whether a string is a palindrome using recursion

Code has been copied
/****************************************
            alphabetacoder.com
C++ program to check whether a string is 
a palindrome or not using recursion
*****************************************/

#include <iostream>
#include <cstring>

using namespace std;

// function to check if a string is a palindrome
int check_palindrome(char * s, int i) {
    // exit condition of recursive call
    if (i == strlen(s) / 2)
        return 1;
    // compare the characters
    // if mismatch found then return 0
    if (s[i] != s[strlen(s) - 1 - i])
        return 0;
    // call function
    return check_palindrome(s, i + 1);
}

int main() {
    // declare character array to store strings
    char str[30];
    // declare variables
    int result;

    // take input of string
    cout << "Enter the string: ";
    cin >> str;

    // call function to check whether
    // str is palindrome
    // function check_palindrome() takes
    // string str and first charcater index 0
    // as parameters
    result = check_palindrome(str, 0);

    // if result = 1, the string is palindrome
    if (result == 1)
        cout << str << " is a palindrome string!" << endl;
    else
        cout << str << " is not a palindrome string!" << endl;

    return 0;
}