Question: 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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!