Stack

16.5 - Bracket balance


In this exercise your task is to create a program that determines whether the brackets in the input text are balanced or not. The brackets are balanced if:

  • There is an equal amount of opening and closing brackets.
  • Each closing bracket matches the last opening bracket which is not closed yet.

The bracket types are:

  • ()
  • []
  • {}
  • <>

Sample input 1

(this text is balanced)

Sample output 1

Balanced

Sample input 2

(this text is not balanced

Sample output 2

Not balanced

Sample input 3

({this text is
not balanced)}

Sample output 3

Not balanced
#include <iostream> #include <string> #include <stack> bool check_bracket_balance() { } int main() { if (check_bracket_balance()) { std::cout << "Balanced" << std::endl; } else { std::cout << "Not balanced" << std::endl; } }