Logic Error

My assignment is to use arrays to construct a program that will determine if the input word is a palindrome or not. However, every input results in the program saying that it is a palindrome. I've tried many solutions but none seem to fix the issue.



//Display Program
#include<iostream>
using namespace std;

int palindrome(char letter[], int size)
{
int i;
int j= size - 1;

for(i = 0; i <= j; i++)
{
if (letter[i] != letter[j])
{
return 0;
}
else
j--;
}
return 1;
}
int main()
{
const int SIZE = 20;
char letter[SIZE];
char repeat;
int i;
int j;
int count = 0;
int size;

do
{
cout << "Enter a word (. to end the word): ";

for(i = 0; i < SIZE; i++)
{
cin >> letter[i];
if (letter[i] == '.')
{
break;
}
count++;
}

for(i = i - 1; i >= 0; i--)
{
cout << letter[i];
}

cout << "\n";

if (palindrome(letter, i))
{
cout << "This word " << "is a palindrome." << endl;
}
else
{
cout << "This word " << "is NOT a palindrome." << endl;
}

cout << "Repeat (Y/N): ";
cin >> repeat;

}while (repeat == 'Y' || repeat == 'y');

//letter[1] = letter[i - 2];
//letter[2] = letter[i - 3];

return 0;
}
Check the value of i before you send it to the palindrome function at line 51.

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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include<iostream>
using namespace std;

int palindrome(char letter[], int size)
{
int i;
int j= size - 1;

for(i = 0; i <= j; i++)
{
if (letter[i] != letter[j])
{
return 0;
}
else
j--;
}
return 1;
}
int main()
{
const int SIZE = 20;
char letter[SIZE];
char repeat;
int i;
int j;
int count = 0;
int size;

do
{
cout << "Enter a word (. to end the word): ";

for(i = 0; i < SIZE; i++)
{
cin >> letter[i];
if (letter[i] == '.')
{
break;
}
count++;
}

for(i = i - 1; i >= 0; i--)
{
cout << letter[i];
}

cout << "\n";

if (palindrome(letter, i))
{
cout << "This word " << "is a palindrome." << endl;
}
else
{
cout << "This word " << "is NOT a palindrome." << endl;
}

cout << "Repeat (Y/N): ";
cin >> repeat;

}while (repeat == 'Y' || repeat == 'y');

//letter[1] = letter[i - 2];
//letter[2] = letter[i - 3];

return 0;
} 
Topic archived. No new replies allowed.