Just did this simple application to check whether a string is a palindrome or not
#include <iostream>
#include <string>
using namespace std;
void main()
{
string str;
cout << "Enter a string: "; getline(cin, str);
cout << "Your string is: "; cout << str << endl;
cout << "The reverse of the string is: ";
int counter = 0;
for (int reverseIdx = str.size() - 1, forwardIdx = 0; reverseIdx >= 0 &&
forwardIdx < str.size(); reverseIdx--, forwardIdx++)
{
cout << str[reverseIdx];
if (str[reverseIdx] == str[forwardIdx])
{
counter++;
}
}
cout << endl;
if (counter == str.size())
{
cout << "The string is a Palindrome!" << endl;
}
else
{
cout << "The string is not a Palindrome." << endl;
}
}
I'm getting a string from the user, then printing out the same string (I know it's not needed in such small programs). Then using a for loop to check if every character when reversed is equal to the same character when read normally.
Then using a counter variable I am checking if it's equal to the string size, if it's then it's palindrome.