Parentheses are important characters in programming – they define the order of operations and organize information. That is, as long as they are properly balanced. Parentheses are balanced when the opening and closing brackets match with one another, and are themselves nested within balanced parentheses.
This is perhaps best illustrated in examples.
- ( ) – balanced: opening is matched with a closing
- ( ( ) – not balanced: one of the opening brackets has no match
- ( [ ) ] – not balanced: the contents inside of ( ) are not balanced
The input file DATA4.txt will contain 5 lines, each no more than 255 characters long. Valid characters are any of the three parenthesis types: (), [], {}, and any alphanumeric characters: a-z0-9.
The output file OUT4.txt will contain 5 lines, each stating either “balanced” or “not balanced” for the supplied expressions. Use the exact string output, all lower case, without quotes.
All of the characters but the parentheses could effectively be ignored. Keep in mind the different types of parentheses – an opening bracket needs to be matched with a closing bracket of the same type.
abc
([{a}b]c)
)(
([)]
{abc]
balanced
balanced
not balanced
not balanced
not balanced