This article will explain different ways to check for a palindrome string in C++ with example programs and their explanations.
A Palindrome string is one that remains the same when reversed.
That is, if you compare the original string and its reversed string, both will be same.
Examples of Palindrome string are madam, malayalam, level, refer, radar etc.
Method 1
This method reads a string from the keyboard and then iterates over it using a for loop.
In every iteration, it compares the characters at the left position with the character at corresponding right position.
#include <iostream> #include <cstring> using namespace std; int main() { // variable to hold the string string s = ""; // declare a variable for checking result later bool isPalindrome = true; cout << "Enter a string" << endl; // read the string getline(cin, s); // get length of string int length = s.length(); // loop over string for(int i = 0; i < length; i++){ // compare characters from start and end if(s[i] != s[length - i - 1]) { isPalindrome = false; break; } } // print result if(isPalindrome) { cout << "String is palindrome" <<endl; } else{ cout << "String is not palindrome" <<endl; } }
The program uses length()
function of String
class to get the length of the string.
Notice the statement if(s[i] != s[length - i - 1])
which compares the character at the left side with the character at the right.
As soon as it finds a mismatch, it sets the bool
variable to false
and terminates the loop.
Output
Enter a string
lel
String is palindrome
Enter a string
abce
String is not palindrome
This method is similar to above method but it uses C-style string to store the string in a character array and it uses two variables to access the characters at the left and right of the string.
Variables
i
and j
are used for left and right characters respectively.i
is initialized to 0 which means that it points to the first character.j
is initialized to length of string – 1, which means that it points to the last character of string.In every loop iteration,
i
is increased while j
is decreased by 1 and the characters at ith and jth indexes are compared.If they are not same at any point, the string is not palindrome.
#include <iostream> #include <cstring> using namespace std; int main() { char s[50]; cout << "Enter a string" << endl; cin.get(s,50); int length = strlen(s); bool isPalindrome = true; for (int i = 0, j = length - 1; i < length; i++, j--) { if(s[i] != s[j]) { isPalindrome = false; break; } } if (isPalindrome) { cout << "String is palindrome" << endl; } else { cout << "String is not palindrome" << endl; } }
Output
Enter a string
codippa
String is not palindrome
Enter a string
madam
String is palindrome
This method uses two string objects. One object contains the string that needs to be checked and second contains the reversed string.
Input string is reversed using a for loop which traverses the string in reverse direction and assigns each character to the second string.
After the loop completes, the second string contains the input string in reverse order. Both these strings are then compared using
compare()
function of String
class.compare()
will return 0 if the strings are same.
#include <iostream> #include <string> using namespace std; int main() { cout << "Enter a string" << endl; // for input string string s = ""; // for reverse string string reverse = ""; // read input string getline(cin, s); int length = s.length(); /* make the reverse string array of same size as input string */ reverse.resize(length); for(int i = 0,j = length-1; i < length; i++,j--){ reverse[i] = s[j]; } // compare input and reversed strings if(s.compare(reverse) == 0) { cout << "String is palindrome" << endl; } else{ cout << "String is not palindrome" <<endl; } }
Notice the use of resize()
function invoked on the reverse string object.
This function is passed the length of input string and is used so that the size of array used to store reversed string becomes the same as the input string and they are compared correctly.
Output
Enter a string
racecar
String is palindrome
Enter a string
apple
String is not palindrome
Hope the article was useful.