So I have to write a program that checks to see if a word entered is a palindrome. I can't really find my error but for some reason it gives me different output everytime
//4 January 2017
#include <iostream>
#include<string>
usingnamespace std;
int main(){
int x=0;
int length=0;
string word;
int palindrome=0;
for (int r=0;r<3;r++){
cout<<"Enter a word to check if it is a Palindrome: ";
cin>>word;
length=word.length();
do{
if(word[x] != word[length-x-1]){
palindrome=-1;
}
x++;
}while(palindrome==0 && x<(length));
if (palindrome==0){
cout<<"The word you entered is a palindrome";
cout<<endl;
}
else{
cout<<endl;
cout<<"The word you entered is not a Palindrome.";
}
}
return 0;
}
. For some reason if i enter a 2 character input the third time it always outputs it as a palindrome even when it isn't.
Another question that I have is how would you enter in phrases instead of words and have it take away the spaces and then tell if it is a palindrome?