Check if String is Palindrome in C++

This article will discuss different ways to check if a string is palindrome or not in C++.

Table of Contents

There are different ways to do this. Let’s discuss them one by one,

Check if a string is palindrome using STL Algorithm equal()

To verify if a string is a palindrome, we need to compare the first half with the reversed second half of the String. For example, if String is of length N, then compare,

  • Character at index 0 with character at index N-1
  • Character at index 1 with character at index N-2
  • Character at index 2 with character at index N-3
  • ..
  • ..
  • Character at index n/2-1 with character at index (N-N/2)

If all the above comparisons return true, it means String is a palindrome.

Standard Template Library in C++ provides a function std::equal(). It compares two ranges for element-wise equality, and if all elements in two ranges are equal, it returns true, otherwise false. We can use this to check if String is a palindrome by matching the first half of String with the reversed second half of String i.e.

#include <iostream>
#include <string>
#include<algorithm>

using namespace std;

/*
    This function returns true
    if a give string is a palindrome,
    otherwise returns false.
*/
bool is_palindrome(string input_str)
{
    int len = input_str.length();
    return equal( input_str.begin(),
                  input_str.begin() + len/2,
                  input_str.rbegin(),
                  input_str.rbegin() + len/2 );
}

int main()
{
    cout<< is_palindrome("abccbad") << endl;
    cout<< is_palindrome("abccba") <<endl;
    cout<< is_palindrome("abcdcba") <<endl;
    cout<< is_palindrome("12abcdcba21") <<endl;
    cout<< is_palindrome("87655778") <<endl;
   
    return 0;
}

Output

0
1
1
1
0

Analysis of the Output:

  • The string “abccbad” is not a palindrome; therefore, it returned false.
  • The string “abccba” is not a palindrome; therefore, it returned true.
  • The string “abcdcba” is not a palindrome; therefore, it returned true.
  • The string “12abcdcba21” is not a palindrome; therefore, it returned true.
  • The string “87655778” is not a palindrome; therefore, it returned false.

Check if String is palindrome using for loop

If you don’t want to use the equal() function, then you can iterate over the String and compare,

  • Character at index 0 with character at index N-1
  • Character at index 1 with character at index N-2
  • Character at index 2 with character at index N-3
  • ..
  • ..
  • Character at index n/2-1 with character at index (N-N/2)

Where N is the size of String, if all the comparisons return True then it means String is a palindrome. For example,

#include <iostream>
#include <string>

using namespace std;

/*
    This function returns true
    if a give string is a palindrome,
    otherwise returns false.
*/
bool is_palindrome(string input_str)
{
    bool result = true;
    int len = input_str.length();
    for(int i = 0 ; i < len/2 ; i++)
    {
        if(input_str[i] != input_str[len -1 -i] )
        {
            result = false;
            break;
        }
    }
    return result;
}

int main()
{
    cout<< is_palindrome("abccbad") << endl;
    cout<< is_palindrome("abccba") <<endl;
    cout<< is_palindrome("abcdcba") <<endl;
    cout<< is_palindrome("12abcdcba21") <<endl;
    cout<< is_palindrome("87655778") <<endl;
   
    return 0;
}

Output

0
1
1
1
0

Check if String is palindrome using reverse()

If the reverse of a string is equal to the original String, it means the String is a palindrome. Let’s see how to do that,

#include <iostream>
#include <string>
#include<algorithm>

using namespace std;

/*
    This function returns true
    if a give string is a palindrome,
    otherwise returns false.
*/
bool is_palindrome(string input_str)
{
    std::string new_str(input_str);
    std::reverse(new_str.begin(), new_str.end());
    return (new_str ==  input_str);
}

int main()
{
    cout<< is_palindrome("abccbad") << endl;
    cout<< is_palindrome("abccba") <<endl;
    cout<< is_palindrome("abcdcba") <<endl;
    cout<< is_palindrome("12abcdcba21") <<endl;
    cout<< is_palindrome("87655778") <<endl;
   
    return 0;
}

Output

0
1
1
1
0

Summary

We learned three different ways to check if a string is a palindrome in C++.

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top