I am working on a palindrome program for class. I've written the program and it works. The issue I'm having is the output. I can't figure out how to change the characters into the number they are associated with. Here is my code:
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main ()
{
string word;
int i;
int length;
int counter = 0;
cout << "Please enter a word." << endl;
getline (cin,word);
cout << "The length of the word is " << word.length() << "." << endl;
length = word.length();
for (i=0;i < length ; i++)
{
cout << "Checking element " << word[i] << " with element " word[length-i-1] << "." << endl;
if (word[i] != word[length-i-1])
{
counter = 1;
break;
}
}
if (counter)
{
cout << "NO: it is not a palindrome." << endl;
}
else
{
cout << "YES: it is a palindrome." << endl;
}
return 0;
}
The output I'm getting displays all the characters of the string and looks like this: my output
Please enter a word
hannah
Checking element h with element h
Checking element a with element a
Checking element n with element n
(etc)
Yes: it is a palindrome.
But, I need the output to display the characters as their placement number in the string, which looks like this:
Please enter a word
hannah
Checking element 0 with element 5
Checking element 1 with element 4
Checking element 2 with element 3
Yes: it is a palindrome.
Any hints or tips would be great. I just feel like I've tried everything I know, and it still won't look right. Thank you!
Instead of using :
cout << "Checking element " << word[i] << " with element " word[length-i-1] << "." << endl;
why not use:
cout << "Checking element " << i << " with element " << (length-i-1) << "." << endl;