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
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
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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started