c++ palindrome problem

Sep 29, 2010 at 11:18pm
thanks
Last edited on Sep 30, 2010 at 10:07pm
Sep 29, 2010 at 11:20pm
A do/while(word != end)?
Sep 30, 2010 at 3:11am
ok, the do/while caused it to loop, but the end statement is not terminating the program. Another problem I have is that the palindromes should not be case sensitive..

Thanks for the help by the way.
Last edited on Sep 30, 2010 at 3:34am
Sep 30, 2010 at 3:59am
#include <iostream>
#include <fstream>
#include <cstring>


using namespace std;

int main(void){
char word[81];
do{
bool palindrome=true;




cout << "Please enter a word" << endl;
cin>>word;
int length = strlen(word);
int(length/2);
if (length>0){
for(int i=0;i<(length);i++)
{
if(word[i]!=word[length-1-i])
palindrome=false;
}
}
if(palindrome==true)
{
cout << "The word is a palindrome" << endl;
}
else
{
cout << "The word is not a palindrome" << endl;
}

}while (word != "END");

return(0) ;

}
Sep 30, 2010 at 5:17am
As I understand your code and the problem you are trying to solve, it seems that your test for a palindrome is incorrect. Dividing by 2 means you will miss odd lettered palindromes like 'level'. You use the code word[i] when I believe you mean to use the code word.length[i]. The latter examines each individual letter of the string in word. The former does not.
Sep 30, 2010 at 5:46am
I don't know if a single character is considered a palindrome or not.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
string word;
do{
bool palindrome=true;

cout << "Please enter a word" << endl;
cin >> word;
int cnt = word.length()/2;
int wordLen = word.length()-1;
for(int i=0; i<cnt; i++, wordLen--) {
  if (word[i] != word[wordLen]) {
    palindrome = false;
    break;
  }
}
palindrome ? cout << "The word is a palindrome\n" : cout << "The word is not a palindrome\n";

}while (word != "END");
Last edited on Sep 30, 2010 at 5:47am
Sep 30, 2010 at 7:17am
How about you just create an infinite loop, then add this after cin:
if (word == "END") break; // or return 0 if you want to quit right away.

It's bad UI to tell the user that the word (END) isn't a palindrome when the user just want's to quit.
Sep 30, 2010 at 7:52am
Hi Fresh Grass, I am working based on OP original code. I agree UI wise it is not intuitive but hey I am not supposed to write the whole program. I am supposed to help him resolve the critical portion of it. I believe the OP should be able to change the UI itself cuz afterall the detection of palindrome has already been done.
Sep 30, 2010 at 12:13pm
hello nonsatisscire,

taken what sohguanh wrote you can use 'reverse' with strings to easily solve that problem:
1
2
3
4
5
6
7
8
9
do{
cout << "Please enter a word" << endl;
cin >> word;
string w2 = word;
reverse(w2.begin(), w2.end());

(word == w2) ? cout << "The word is a palindrome\n" : cout << "The word is not a palindrome\n";

}while (word != "END");
Sep 30, 2010 at 1:08pm
Thanks for the help! The program works fine as is, but for some reason it still won't terminate when I enter "END"..

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
#include <iostream>
#include <fstream>
#include <cstring>


using namespace std;

int main(void){
char word[81];
do{
bool palindrome=true;


cout << "Please enter a word" << endl;
cin>>word;
int length = strlen(word);
for (int i=0; i<length; i++){
word[i] = toupper(word[i]); 
}
int(length/2);
if (length>0){
for(int i=0;i<(length);i++)
{
if(word[i]!=word[length-1-i]) 
palindrome=false;
} 
}
if(palindrome==true)
{ 
cout << "The word is a palindrome" << endl; 
}
else
{
cout << "The word is not a palindrome" << endl; 
}

} while (word!="END");

return(0) ;

}
Last edited on Sep 30, 2010 at 1:12pm
Sep 30, 2010 at 1:45pm
if you use that char word[81]; this line while (word!="END"); compares 2 pointers which are always different. then you must use (0 == strcmp(word, "END"))
Sep 30, 2010 at 5:09pm
coder777- this doesn't seem to fix the problem..
Oct 1, 2010 at 5:34am
well, then use (0 != strcmp(word, "END"))
Topic archived. No new replies allowed.