Stack help. I need help with my lab assignment. Complete a method for a class na
ID: 3592829 • Letter: S
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 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<class ItemType>
class StackInterface
{
public:
/** Sees whether this stack is empty.
@return True if the stack is empty, or false if not. */
virtual bool isEmpty() const = 0;
/** Adds a new entry to the top of this stack.
@post If the operation was successful, newEntry is at the top of the stack.
@param newEntry The object to be added as a new entry.
@return True if the addition is successful or false if not. */
virtual bool push(const ItemType& newEntry) = 0;
/** Removes the top of this stack.
@post If the operation was successful, the top of the stack
has been removed.
@return True if the removal is successful or false if not. */
virtual bool pop() = 0;
/** Returns the top of this stack.
@pre The stack is not empty.
@post The top of the stack has been returned, and
the stack is unchanged.
@return The top of the stack. */
virtual ItemType peek() const = 0;
}; // end StackInterface
#endif
ArrayStack.h
#ifndef _ARRAY_STACK
#define _ARRAY_STACK
#include "StackInterface.h"
const int MAX_STACK = 50;
template<class ItemType>
class ArrayStack : public StackInterface<ItemType>
{
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 <string>
#include <cctype>
#include "ArrayStack.h"
using namespace std;
#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<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 ();
/**
* 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 <cassert> // For assert
#include "ArrayStack.h" // Header file
template<class ItemType>
ArrayStack<ItemType>::ArrayStack() : top(-1)
{
} // end default constructor
// Copy constructor and destructor are supplied by the compiler
template<class ItemType>
bool ArrayStack<ItemType>::isEmpty() const
{
return top < 0;
} // end isEmpty
template<class ItemType>
bool ArrayStack<ItemType>::push(const ItemType& newEntry)
{
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<class ItemType>
bool ArrayStack<ItemType>::pop()
{
bool result = false;
if (!isEmpty())
{
top--;
result = true;
} // end if
return result;
} // end pop
template<class ItemType>
ItemType ArrayStack<ItemType>::peek() const
{
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<char> theStack;
string current;
bool palin = false;
for(int i=0; i<phrase.length(); i++)
theStack.push(phrase[i]);
// pop each character of the stack back onto reversePhrase with a loop and using
for(int i=0; i<phrase.length(); i++){
current = theStack.peek();
theStack.pop();
// reversePhrase as an indexable array of characters
reversePhrase+=current;
}
//test for palindrome
for (int i=0; i<phrase.length(); i++){
if(!ispunct(phrase[i]) && !isblank(phrase[i])) // this logic is off.
{
//if not punctuation or blank
toupper(phrase[i]);
toupper(reversePhrase[i]);
if(phrase[i] != reversePhrase[i])
{
palin = false;
return palin;
}
else
palin = true;
}
}
return palin;
}
StackMain.cpp
#include <cstdlib>
#include <iostream>
#include <string>
#include <cctype>
#include <cassert>
#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;
}
Explanation / Answer
#ifndef _ARRAY_STACK
#define _ARRAY_STACK
#include "StackInterface.h"
const int MAX_STACK = 50;
template<class ItemType>
class ArrayStack : public StackInterface<ItemType>
{
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 <string>
#include <cctype>
#include "ArrayStack.h"
using namespace std;
#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<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 ();
/**
* 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
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.