C++ Program to Check if a Given String is Palindrome or Not
Last Updated :
04 Jun, 2025
A string is said to be palindrome if the reverse of the string is the same as the original string. In this article, we will check whether the given string is palindrome or not in C++.
Examples
Input: str = "ABCDCBA"
Output: "ABCDCBA" is palindrome
Explanation: Reverse of the string str is "ABCDCBA". So, it is palindrome.
Input: str= "ABCD"
Output: "ABCD" is NOT palindrome
Explanation: Reverse of string str is "DCBA". So, it is not palindrome.
There are various different methods to check whether the string is palindrome or not in C++.
By Comparing with Reversed String
The simplest way to check if a given string is a palindrome is by reversing the string and comparing it with the original string. If they are equal, then the string is palindrome. Otherwise, it is NOT palindrome. We can use the std::reverse() function to reverse the string in C++.
Code Implementation
C++
// C++ program to check whether the given string
// is palindrome or not using reverse() function
#include <bits/stdc++.h>
using namespace std;
void isPalindrome(string str) {
// Stores the reverse of the string s
string rev = str;
// Reverse the string
reverse(rev.begin(), rev.end());
// If rev is equal to str
if (str == rev)
cout << "\"" << str
<< "\" is palindrome." << endl;
// If rev is NOT equal to str
else
cout << "\"" << str
<< "\" is NOT palindrome." << endl;
}
int main() {
// Checking if strings given strings are palindrome
isPalindrome("ABCDCBA");
isPalindrome("ABCD");
return 0;
}
Time Complexity: O(n), where n is the length of string.
Auxiliary Space: O(n)
Using Two Pointer Method
In this method, we place the two index pointers, left and right at the starting and ending character of the string respectively. After that, we start matching the character present at left and right position.
- If the characters match, we will increment left and decrement right.
- This process will repeat till left is less than right. If left will become greater than or equal to right, then the string is palindrome.
- If at any point the character does not match, then string is not palindrome.
Code Implementation
C++
// C++ program to check whether the string
// is palindrome or not using loop
#include <bits/stdc++.h>
using namespace std;
void isPalindrome (string str) {
// Specify the starting and ending indexes
int left = 0, right = str.size() - 1;
// Flag to terminate the loop if mismatch
// characters found
bool flag = true;
// Till the left is less than right
while (left < right) {
// If any character not match, break the
// loop, string is not palindrome
if (str[left] != str[right]) {
flag = false;
break;
}
left++;
right--;
}
if (flag)
cout << "\"" << str
<< "\" is palindrome." << endl;
else
cout << "\"" << str
<< "\" is NOT palindrome." << endl;
}
int main() {
// Checking if given strings are palindrome
isPalindrome("ABCDCBA");
isPalindrome("ABCD");
return 0;
}
Time Complexity: O(n), where n is the length of string.
Auxiliary Space: O(1)
Using Recursion
This method basically implements the two-pointer approach using recursion. We pass the left and right index pointer to the recursive function that increment keep increments the left and decrement the right till the mismatched pair of character is found (condition for non-palindrome string) or right becomes less than left (condition for palindrome string).
Code Implementation
C++
// C++ Prgram to check whether the given string is
// palindrome or not using recursion
#include <iostream>
#include <cstring>
using namespace std;
// Recursive helper function to check if the string
// is a palindrome
bool palinHelper(const string& str, int left,
int right) {
// If the start and end pointers cross each other,
// it means all characters have matched
if (left >= right)
return true;
// If characters don't match, the string is not
// a palindrome
if (str[left] != str[right])
return false;
// Recursively check for the rest of the string
return palinHelper(str, left + 1, right - 1);
}
// Function to check if the string is a palindrome
void isPalindrome(string str) {
// Calling the recursive function to check
// palindrome string
if (palinHelper(str, 0, str.length() - 1))
cout << "\"" << str << "\" is palindrome."
<< endl;
else
cout << "\"" << str << "\" is NOT palindrome."
<< endl;
}
int main() {
// Checking if the given strings are palindrome
isPalindrome("ABCDCBA");
isPalindrome("ABCD");
return 0;
}
Output"naman" is palindrome.
"vivek" is NOT palindrome.
Similar Reads
Palindrome String Coding Problems A string is called a palindrome if the reverse of the string is the same as the original one.Example: âmadamâ, âracecarâ, â12321â.Palindrome StringProperties of a Palindrome String:A palindrome string has some properties which are mentioned below:A palindrome string has a symmetric structure which m
2 min read
Palindrome String Given a string s, the task is to check if it is palindrome or not.Example:Input: s = "abba"Output: 1Explanation: s is a palindromeInput: s = "abc" Output: 0Explanation: s is not a palindromeUsing Two-Pointers - O(n) time and O(1) spaceThe idea is to keep two pointers, one at the beginning (left) and
13 min read
Check Palindrome by Different Language
Easy Problems on Palindrome
Sentence Palindrome Given a sentence s, the task is to check if it is a palindrome sentence or not. A palindrome sentence is a sequence of characters, such as a word, phrase, or series of symbols, that reads the same backward as forward after converting all uppercase letters to lowercase and removing all non-alphanumer
9 min read
Check if actual binary representation of a number is palindrome Given a non-negative integer n. The problem is to check if binary representation of n is palindrome or not. Note that the actual binary representation of the number is being considered for palindrome checking, no leading 0âs are being considered. Examples : Input : 9 Output : Yes (9)10 = (1001)2 Inp
6 min read
Print longest palindrome word in a sentence Given a string str, the task is to print longest palindrome word present in the string str.Examples: Input : Madam Arora teaches Malayalam Output: Malayalam Explanation: The string contains three palindrome words (i.e., Madam, Arora, Malayalam) but the length of Malayalam is greater than the other t
14 min read
Count palindrome words in a sentence Given a string str and the task is to count palindrome words present in the string str. Examples: Input : Madam Arora teaches malayalam Output : 3 The string contains three palindrome words (i.e., Madam, Arora, malayalam) so the count is three. Input : Nitin speaks malayalam Output : 2 The string co
5 min read
Check if characters of a given string can be rearranged to form a palindrome Given a string, Check if the characters of the given string can be rearranged to form a palindrome. For example characters of "geeksogeeks" can be rearranged to form a palindrome "geeksoskeeg", but characters of "geeksforgeeks" cannot be rearranged to form a palindrome. Recommended PracticeAnagram P
14 min read
Lexicographically first palindromic string Rearrange the characters of the given string to form a lexicographically first palindromic string. If no such string exists display message "no palindromic string". Examples: Input : malayalam Output : aalmymlaa Input : apple Output : no palindromic string Simple Approach: 1. Sort the string charact
13 min read