In this blog post, you will learn how to write a C++ Program to check if a given String is Palindrome or not. But before writing the C++ code if a given String is Palindrome or not first let’s understand what a palindrome string is.
A string is called a palindrome string if the reverse of the given string is the same as the original string. For example, The string “amlma
” is a palindrome because the reverse of the string is the same as the original string.
Approach 1:
Here I will use the inbuilt reverse() function to check whether a string is palindrome. Let’s consider the below example,
In this approach, you have to follow the below-mentioned steps:
- Copy the string str to another temporary string copyString.
- Reverse the string copyString.
- Now check if the string str equals the string copyString, and then print “String is Palindrome”. Otherwise, print “String is not Palindrome“.
#include <vector> #include <iostream> // algorithm needs to be included to use std::reverse() #include <algorithm> using namespace std; int main() { string str = "amlma"; //Copy the original string string copyString = str; //Use the reverse to reverse string reverse(copyString.begin(), copyString.end()); // If str is equal to copyString if(str == copyString) { cout << "String is Palindrome" <<endl; } else { cout << "String is Not Palindrome" <<endl; } return 0; }
Output:
String is Palindrome
Approach 2: By Traversing the String
It is an optimized way to check whether a string is palindrome. In this approach, you have to follow the below-mentioned steps:
- Iterate over the range [0, L/2], where L is the length of the string.
- Make two indexes l (low) and h (high) respectively.
- Initialized the l with 0 and h with (L-1), where 0 is the index of the first element and (L-1) is the index of the last element.
- Using the variables l and h in each iteration check if the characters at index I and h are equal or not.
- If equal increment l and decrement the h otherwise break the loop.
#include <vector> #include <iostream> // algorithm needs to be included to use std::reverse() #include <algorithm> using namespace std; bool isPalindrome(string S) { int l = 0; int h = (S.length() - 1); bool isPalindromeString = true; // Iterate over the range [0, N/2] for (int i = 0; i < S.length() / 2; ++i) { // If S[l] is not equal to // the S[h] if (S[l++] != S[h--]) { isPalindromeString = false; break; } } return isPalindromeString; } int main() { string str = "amlma"; const bool isPalindromeString = isPalindrome(str); if(isPalindromeString) { cout << "String is Palindrome" <<endl; } else { cout << "String is Not Palindrome" <<endl; } return 0; }
Output:
String is Palindrome
Approach 3: By Using std::equal()
You can check whether the string is palindrome or not by comparing the first half of the string with the latter half, in reverse. Consider the below example code,
#include <vector> #include <iostream> // algorithm needs to be included to use std::reverse() #include <algorithm> using namespace std; int main() { string str = "amlma"; const bool ret = equal(str.begin(), str.begin() + str.size()/2, str.rbegin()); if(ret) { cout << "String is Palindrome" <<endl; } else { cout << "String is Not Palindrome" <<endl; } return 0; }
Recommended Articles for you:
- C++ Programming Courses And Tutorials
- Operator Overloading in C++ with some FAQ.
- MCQs on Virtual function in C++.
- MCQs on C++ Templates.
- Introduction of reference in C++.
- Use of mutable keywords in C++.
- Best electronic kits for programmers.
- References and const in C++ with example programs.
- C++ Interview Questions with Answers.
- List of some Best C++ Books, you must see.