Palindrome Program

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

here is the code that i have
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//4 January 2017

#include <iostream>
#include<string>
using namespace 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;
}
Last edited on
An easy way to do it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <string>

using namespace std;

bool isPalindome(const string& s)
{
  string reversed(s.rbegin(), s.rend());

  return s == reversed;
}

int main()
{
  string input;

  for(int i = 0; i < 3; i++)
  {
    cout << "\nEnter word: ";
    getline(cin, input);
    if (isPalindome(input))
      cout << "\n " << input << " is a palindrome";
    else
      cout << "\n " << input << " isn't a palindrome";
  }
  cout << "\n\n";
  system("pause");
  return 0;

}
The assignment: palindrome = 0; needs to occur at the beginning of each loop iteration. It currently only happens one time outside the loop.
thanks
cire
. 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?
Last edited on
Topic archived. No new replies allowed.