In this C++ tutorial, you will learn how to write a C++ Program to check if the given string is a Palindrome string or not.
C++ Palindrome String
A string is a Palindrome, if this string value and its reversed value are same.
Program
In the following program, we read a string into str
, and check if this string str
is Palindrome or not.
main.cpp
</>
Copy
#include <iostream>
using namespace std;
int main() {
string str, reversed;
cout << "Enter a string : ";
cin >> str;
//make a copy of given string to reversed
reversed = str;
//and reverse the string
reverse(reversed.begin(), reversed.end());
//check if str equals reversed
if (str == reversed) {
cout << "The string is a Palindrome." << endl;
} else {
cout << "The string is not a Palindrome." << endl;
}
}
Output
Enter a string : moosoom
The string is a Palindrome.
Program ended with exit code: 0
Enter a string : apple
The string is not a Palindrome.
Program ended with exit code: 0
Conclusion
In this C++ Tutorial, we learned how to check if given string is a Palindrome or not.