← Back to STL Containers
Question 511
Balanced Round Brackets
Balanced Round Brackets
Write a functionbool balanced(const string& s)that returns true when every(insis closed by a matching)in the correct order, and false otherwise. The string contains only the characters(and). The loop over the string and the if/else branches are already written; fill in the three commented lines: - on(: push it onto the stack - on): if the stack is empty return false, otherwise pop - after the loop: return true only if the stack is empty The tests printbalancedwhen the function returns true andnot balancedwhen it returns false, one line per string tested. For example(()())prints the output shown in Expected Output. Do NOT write a main function.
Expected Output:
balanced not balanced not balanced
= newline. The output ends with a newline — print one after the last line.
Hints4 available
Sign in to use hintsTopics:
STL ContainersStacks
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Shift+Alt+F to format · Ctrl+Space to expand shortcuts (cout, fori)
Your Output
Run your code to see the output here...
Test Cases
Run your code to see test case results.