You know what a palindrome number is. The same concept can be used for strings. Basically, palindrome string is a string that is same if you read it forwards or backwards. Some palindrome strings examples are "dad", "radar", "madam" etc. You will have to write a program, that tells us whether the input string is palindrome or not.
Hint: Reverse the original string and compare the resultant with the original.
#include <iostream>
#include <string>
using namespace std;
int main() {
string input;
cout << "Please enter a string: ";
cin >> input;
if (input == string(input.rbegin(), input.rend())) {
cout << "Your string \'"<< input << "\' is a palindrome" << endl;
} else {
cout << "Your string \'"<< input << "\' is not a palindrome" << endl;
}
return 0;
}
Comments
Leave a comment