Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ This program uses a stack to determine whether a string entered at the keyboard has balanced parenthesis, ( ). The string is balanced when

C++ This program uses a stack to determine whether a string entered at the keyboard has balanced parenthesis, ( ). The string is balanced when each right parenthesis occurring in the string is matched with a preceding left parenthesis. Your task is to complete the code that uses the stack as it checks to see if a string has balanced parenthesis (in the box or boxes). Use the program's given constructs and variables only.

#include

#include

#include // STL stack

using namespace std;

bool isBalanced(string, int); // Prototype

int main()

{

string str;

// Tell user what program does

cout << "This program checks a string to see "

<< "if its parentheses are properly "

<< "balanced.";

// Get String from user

cout << " Type in a string with some parenthesis: ";

getline(cin, str);

// Check the string and report

if (isBalanced(str, str.length()))

cout << " The string has balanced parentheses. ";

else

cout << " The string does not have balanced parentheses. ";

return 0;

}

// *************************************************************

// Checks to see if a string has balanced parenthesis. *

// *************************************************************

bool isBalanced(string str, int size)

{

bool status;

stack charStack;

for (int k = 0; k < size; k++)

{

switch(str[k])

{ // YOUR CODE starts HERE ...

// YOUR CODE ends HERE ... } } if (charStack.empty()) status = true; else status = false; return status; }

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

Successful Keyword Searching Initiating Research On Popular Topics Using Electronic Databases

Authors: Randall MacDonald, Susan MacDonald

1st Edition

0313306761, 978-0313306761

More Books

Students also viewed these Databases questions

Question

4. Active development of work communities.

Answered: 1 week ago