Question: Stack help. I need help with my lab assignment. Complete a method for a class named Palindrome that evaluates a string phrase to determine if
Stack help. I need help with my lab assignment.
Complete a method for a class named Palindrome that evaluates a string phrase to determine if the phrase is a palindrome or not. A palindrome is a sequence of characters that reads the same both forward and backward. When comparing the phrase to the same phrase with the characters in reverse order, an uppercase character is considered equivalent to the same character in lowercase, and spaces and punctuation are ignored.
The main() method in the application StackMain uses the overloaded constructor to declare several objects and initialize them to various phrases to test your program for correctness. For each test phrase, the application outputs a message about whether the phrase is a palindrome or not (based on the value returned by evalPalindrome()) the original phrase, and the phrase in reverse order (using the value of reversePhrase changed by evalPalindrome().)
For this lab assignment, implement the evalPalindrome() method, as defined in the Javadoc comments found inside the class definition above. The method you write MUST make use of ArrayStack to reverse the order of the original phrase and to determine if the phrase is a palindrome or not. In other words, the method you write must call the methods in the ArrayStack class to reverse the order of the original phrase and to determine if it is a palindrome.
Note that a library named cctype is included in Palindrome.h. This library contains methods like toupper(), tolower(), isalpha(), and more, which you may find useful. You may also need methods in the cstring, and string libraries. For more information on these libraries, check Appendix H in the textbook (P805-808).
Only the javadoc comments and the body of the evalPalindrome() method should be modified. No other modifications to the included files should be made.
The program MUST output the result as the following:
This phrase IS a palindrome!
Original phrase: kayak
Reverse phrase: kayak
This phrase is NOT a palindrome.
Original phrase: Read dear
Reverse phrase: raed daeR
This phrase IS a palindrome!
Original phrase: Madam, I'm Adam
Reverse phrase: madA m'I ,madaM
This is NOT a palindrome.
Original phrase: The End
Reverse phrase: dnE ehT
This phrase IS a palindrome!
Original phrase: 2Racecar2
Reverse phrase: 2racecaR2
But my output looks like this and I don't know how to fix it.
This phrase IS a palindrome! Original phrase: kayak Reverse phrase: kayak
This phrase is NOT a palindrome. Original phrase: Read dear Reverse phrase: raed daeR
This phrase is NOT a palindrome. Original phrase: Madam, I'm Adam Reverse phrase: madA m'I ,madaM
This phrase is NOT a palindrome. Original phrase: The End Reverse phrase: dnE ehT
This phrase is NOT a palindrome. Original phrase: 2Racecar2 Reverse phrase: 2racecaR2
StackInterface.h
#ifndef _STACK_INTERFACE #define _STACK_INTERFACE
template
ArrayStack.h
#ifndef _ARRAY_STACK
#define _ARRAY_STACK
#include "StackInterface.h"
const int MAX_STACK = 50;
template
class ArrayStack : public StackInterface
{
private:
ItemType items[MAX_STACK]; // Array of stack items
int top; // Index to top of stack
public:
ArrayStack(); // Default constructor
bool isEmpty() const;
bool push(const ItemType& newEntry);
bool pop();
ItemType peek() const;
}; // end ArrayStack
#include "ArrayStack.cpp"
#endif
Palindrome.h
#include
#ifndef PALINDROME_H #define PALINDROME_H class Palindrome { private: /** * stores the letters in the string while it is being evaluated * to determine if it is a palindrome */ ArrayStack
/** * original phrase to evaluate to determine if it is a palindrome */ string phrase;
public: /** * Default constructor. Initializes expression to an empty string. */ Palindrome ();
/** * Overloaded constructor that initializes the phrase. * @param - newPhrase - original phrase tested to determine if it is a * palindrome */ Palindrome (string 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 - true if phrase is a palindrome; false otherwise * @param reversePhrase - orginal phrase in reverse order, including * all puncutation and spaces */ bool evalPalindrome (string& reversePhrase); }; #endif
ArrayStack.cpp
#include
#include "ArrayStack.h" // Header file
template
ArrayStack
{
} // end default constructor
// Copy constructor and destructor are supplied by the compiler
template
bool ArrayStack
{
return top < 0;
} // end isEmpty
template
bool ArrayStack
{
bool result = false;
if (top < MAX_STACK - 1) // Does stack have room for newEntry?
{
top++;
items[top] = newEntry;
result = true;
} // end if
return result;
} // end push
template
bool ArrayStack
{
bool result = false;
if (!isEmpty())
{
top--;
result = true;
} // end if
return result;
} // end pop
template
ItemType ArrayStack
{
assert(!isEmpty()); // Enforce precondition
// Stack is not empty; return top
return items[top];
} // end peek
// End of implementation file.
Palindrome.cpp
#include "Palindrome.h" using namespace std;
Palindrome::Palindrome () { phrase = ""; }
Palindrome::Palindrome (string newPhrase) { phrase = newPhrase; } // After completing this method, submit the entire .cpp // file as your solution for Lab 5. See method description in // Palindrome.h
bool Palindrome::evalPalindrome (string& reversePhrase) { ArrayStack
string current;
bool palin = false;
for(int i=0; i // pop each character of the stack back onto reversePhrase with a loop and using for(int i=0; i //test for palindrome for (int i=0; i return palin; } StackMain.cpp #include #include #include #include #include #include "Palindrome.h" using namespace std; void testForPalindrome(string origPhrase); int main (void) { const int NUM_PHRASES = 5; string allPhrases[NUM_PHRASES] = {"kayak", "Read dear", "Madam, I'm Adam", "The End", "2Racecar2"}; for (int i = 0; i < NUM_PHRASES; i++) testForPalindrome(allPhrases[i]); return EXIT_SUCCESS; } void testForPalindrome(string origPhrase) { string reversePhrase; bool isPalindrome; Palindrome phrase(origPhrase); if (phrase.evalPalindrome(reversePhrase)) { cout << "This phrase IS a palindrome!" << endl; } else { cout << "This phrase is NOT a palindrome." << endl; } cout << "Original phrase: " << origPhrase << endl; cout << "Reverse phrase: " << reversePhrase << endl << endl; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
