Balanced Multiple Delimiters
Balanced Multiple Delimiters
A string may use more than one type of delimiter to bracket information into
“blocks.” For example, A string may use braces {}, parentheses ()~ and brackets [ ] as
delimiters. A string is properly delimited if each right delimiter is matched with a preceding
left delimiter of the same type in such a way that either the resulting blocks of
information are disjoint, or one of them is completely nested within the other. Write a C++
program that uses a single stack to check whether a string containing braces, parentheses,
and brackets is properly delimited.
Answer:
// Balanced Multiple Parentheses
// This program checks a string to see if it contains
// Properly balanced parentheses.
#include <iostream>
#include <string>
#include <stack> // STL stack
using namespace std;
bool isBalanced(string s); // Prototype
int main()
{
// Tell user what program does
cout << "This program checks a string to see if its parentheses are properly \n";
cout << "balanced.";
// Get String from user
string str;
cout << "\nType in a string with some parentheses:\n";
getline(cin, str);
// Check the string and report
if (isBalanced(str))
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. *
// It works like this: Whenever you find a left delimiter, stack the *
// corresponding right delimiter. Then whenever a right delimiter *
// is found, you look to match it with the current top of the stack. *
// If we get to the end of the string and the stack is not empty. *
// *********************************************************************
bool isBalanced(string str)
{
stack<char> charStack;
char expected;
for (unsigned int k = 0; k < str.length(); k++)
{
switch(str[k])
{
case '(' : charStack.push(')'); break;
case '{' : charStack.push('}'); break;
case '[' : charStack.push(']'); break;
case ')' :
case ']' :
case '}' :
expected = charStack.top();
charStack.pop();
// See if you have what you were expecting
if (expected != str[k])
return false;
break;
default: break;
}
}
if (charStack.empty())
return true;
else
return false;
}
Leave a reply