Question: The user for a string to be entered from the keyboard. The string may contain any characters except whitespace (that is, no spaces, tabs, or
The user for a string to be entered from the keyboard. The string may contain any characters except whitespace (that is, no spaces, tabs, or newlines). Among the characters will be enclosures, that is, parentheses, square brackets, and curly braces. Your program will scan the string to determine whether those enclosures are balanced with respect to each other. Very simply, ignore any non-enclosure characters. When a left enclosure character is encountered, push it onto a stack. When a right enclosure character is encountered, pop the left enclosure off the stack and compare to see whether they match, for example, a right parenthesis only matches a left parenthesis. If they don't match, print an error message indicating the position in the string where the mismatch was found and exit the program. If they match, continue scanning the string. Once the string has been completely scanned, check whether the stack is empty. If not, print an error message about unmatched left enclosures. Otherwise, print a message indicating that the string is balanced. Write the code.
Step by Step Solution
3.43 Rating (156 Votes )
There are 3 Steps involved in it
Python code implementation as follows def isbalancedstring stack leftenclosures rightenclosures enclosuremap for i char in enumeratestring if char in ... View full answer
Get step-by-step solutions from verified subject matter experts
