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;

   }

Edited 2 Years Ago by ashley.vanhoesen.7

Why are you using ...

"ArrayStack.h"

It would seem much better to use the C++ STL stack (or list) to hold each char ... then we can 'talk' ...

Also ... a test program ... and your results with your input vs what you expect to get.

Edited 2 Years Ago by David W: fixed typos ...

Everything is fine except for the part where you test the palindrome. You need to break out of the loop as soon as you find a mismatching character. Currently, the way it is, you are going to loop until the last character, and the bool isPalindrome will only reflect the last character (if it matches or not). You can use this loop instead to replace that palindrome checking loop:

    while(k <= tempPLength && k <= tempRLength)
    {
       if(tempOrig[k] == tempRev[k])
          k++;
       else
          return false; // return false on the first mismatch.
    }
    return true;
This article has been dead for over six months. Start a new discussion instead.