CJCoding With Joseph
15free / daySign inCreate account

Balanced Round Brackets

Write a function bool balanced(const string& s) that returns true when every ( in s is 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 print balanced when the function returns true and not balanced when 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
Topics:
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.