I am supposed to make a palindrome program using stacks, but my function keeps returning false. Here's the code:
Main:
int main()
{
char word[50];
cin >> word;
cout << strlen(word);
if (checkIfPalindrome(word) == true) {
cout << endl << "Is a palindrome.";
}
else
{
cout << endl << "Not palindrome.";
}
getch();
}
bool checkIfPalindrome(char word[])
{
charStack reverseWord;
bool fail;
reverseWord.top = strlen(word);
for (unsigned int i = 0; i < strlen(word); i++)
{
reverseWord.push(word[i]);
}
cout << endl << reverseWord.array << endl;
for (unsigned int j = 0; j < strlen(word); j++)
{
if (word[j] != reverseWord.pop())
{
return false;
}
}
return true;
}
Definition of push and pop:
int charStack::push(char item)
{
if (!full()) {
array[top++] = item;
}
else
{
return -1; //Error
}
return 0; //Success
}
char charStack::pop()
{
char deletedItem;
if (!empty()) {
deletedItem = array[top];
top--;
}
else
{
return -1; //Error
}
return deletedItem;
}