I need to remove the spaces and punctuation from the original string and the reverse string, then put the letters only into temp strings for comparision. When I run the output as it is now, I get 0000 for the temp strings. How do I fix this?
Everything else is working properly.
Here is the header:
/**
* @file Palindrome.h
*/
#include <string>
#include <cctype>
#include "ArrayStack.h"
using namespace std;
class Palindrome
{
private:
/**
* stores the letters in the string while it is being evaluated
* to determine if it is a palindrome
*/
ArrayStack<char> letters;
/**
* original phrase to evaluate to determine if it is a palindrome
*/
string phrase;
public:
/**
* Default constructor. Initializes expression to an empty string.
*/
Palindrome ()
{
phrase = "";
}
/**
* Overloaded constructor that initializes the phrase.
* @param - newPhrase - original phrase tested to determine if it is a
* palindrome
*/
Palindrome (string newPhrase)
{
phrase = newPhrase;
}
/**
* Evaluates the phrase to determine if it is a palindrome. Uses
* a stack to reverse the order of the letters in the phrase and
* to determine if the original phrase is a palindrome. A palindrome
* is a sequence of letters that reads the same forward and backward;
* however, all spaces and punctuation are ignored.
* @return isPalindrome - true if phrase is a palindrome; false
* otherwise
* @param reversePhrase - orginal phrase in reverse order, including
* all puncutation and spaces
*/
bool evalPalindrome (string& reversePhrase);
};
Here is the cpp:
bool Palindrome::evalPalindrome (string& reversePhrase)
{
char ch;
int phraseLength = phrase.length();
int i = 0;
int j = 0;
int k = 0;
bool isPalindrome;
int revLength = reversePhrase.length();
string tempRev;
string tempOrig;
int tempPLength = tempOrig.length();
int tempRLength = tempRev.length();
while(i < phraseLength)
{
ch = phrase[i];
letters.push(ch);
i++;
}//end while
while(!letters.isEmpty())
{
reversePhrase += letters.peek();
letters.pop();
j++;
}//end while
while(i <= phraseLength)
{
if(ispunct(phrase[i]) || phrase[i] == ' ')
{
i++;
}
else
{
char c = phrase[i];
tempOrig += tolower(c);
i++;
}
}//end while
while(i <= revLength)
{
if(ispunct(reversePhrase[i]) || reversePhrase[i] == ' ')
{
i++;
}
else
{
char c = reversePhrase[i];
tempRev += tolower(c);
i++;
}
}//end while
while(k <= tempPLength && k <= tempRLength)
{
char curLetter = tempOrig[k];
if(curLetter == tempRev[k])
{
isPalindrome = true;
k++;
}
else
{
isPalindrome = false;
k++;
}
}
return isPalindrome;
}