Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ Palindrome Stack HELP Below is the starter file palindromeStack.cpp #include #include using namespace std; // STEP 4 // STEP 5 // STEP 6 int

C++ Palindrome Stack HELP

Below is the starter file palindromeStack.cpp

#include #include using namespace std;

// STEP 4

// STEP 5

// STEP 6

int main() { // STEP 2 string inputStr; bool result = true;

// STEP 3

// STEP 7

cout

cin.ignore(); cin.get();

return 0; } // end main

Here are the details to finish the lab

image text in transcribed

image text in transcribed

image text in transcribed

Lab: Palindrome Stack Prerequisites: Chapter 6 In this lab you will create a simple program which uses a stack to determine if a string is a palindrome. The C++ Standard Template Library (STL) includes already implemented stack template classes we can use. You can find the general C++ reference for the STL stack here. There is some brief sample code here. Note that where the book ADT Stack uses the peek ) method to return (but not remove) the top item on the stack, the STL stack uses the top ) method. peek () and top () are functionally the same. The Algorithm We can use a stack to test if a string is a palindrome by first pushing each character of the string onto the stack (which results in the last character in the string at the top of the stack) then pulling the characters off the stack one-by-one and comparing them with the string. In other words, we're using the stack to create a completely backward version of the string which we then compare one character at a time to the original string. Given a string theString and stack theStack, the pseudocode algorithm to use a stack to determine if S is a palindrome is: for i0 through theString.length) - 1 push (theString[i]) isPalindrome-true while n library. Step 2 Fnd the comment// STEP 2 and replace it with the code that creates a pointer called stackPtr that points to a new STL stack of char items in free space. The STL stack is a template class, so you'll use the to specify the type of data it can contain. Step 3 Insert the code to prompt the user for a string that includes no punctuation and store it into inputStr. Note we want this to work for multiword palindromes, not just single word. So they should be able to enter "dad" but also "a nut for a jar of tuna". Hint: use getLine ) You don't need to do any validity checking on their input, for this lab you may assume they will not enter punctuation Test your program at this point. Regardless of what string you input it should say the string is a palindrome because the variable result is defaulted to true. Step 4 We're going to implement the algorithm given above, but before we do we want a way to clear an STL stack to make sure we start with an empty stack. Insert a void function called clearStack () which receives as a parameter which is a pointer to an STL stack of chars. The function should then pop () the stack until it is empty. You can tell if an STL stack is empty with its empty () function. For the purposes of this lab you may insert the full function definition above main () Step 5 Add the function stripSpaces () which takes a string as input and returns that string with the spaces removed For the purposes of this lab you may insert the full function definition above main () Step 6 Insert the code for the function testPal that implements the palindrome testing algorithm above. testPal ) should: 1. Receive 2 parameters a. One a pointer to an STL stack of chars. b. One is a string. 2. Pass the stack pointer to clearStack () before using it to make sure the stack is empty. 3. Pass the string to stripSpaces ) to remove the spaces in the string. 4. Implement the algorithm given above to determine if the given string is a palindrome. Don't forget that STL stack doesn't have peek),it has top )! 5. Return true if the string is a palindrome, otherwise false. For this lab you may include the full function definition above main () Insert a call to testPal passing stackPtr and inputStr as arguments. Store the return value in result. Step 8 You should have a working program at this point. Make sure to test your program thoroughly to ensure there are no bugs

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Understanding Oracle APEX 5 Application Development

Authors: Edward Sciore

2nd Edition

1484209893, 9781484209899

Students also viewed these Databases questions