Code:
#include <bits/stdc++.h>
using namespace std;
bool BalancedBraces(string expr) // Function to check if braces
are balanced
{
stack<char> s;
for (int i = 0; i < expr.length(); i++)
// Traversing the Expression
{
if (expr[i] == '{')
s.push(expr[i]);
// Push '{' in the stack
else if (expr[i] == '}')
s.pop(); // Pop
'{' from the stack
}
if(s.empty())
return true;
else
return false;
}
int main()
{
string expr;
cout << "Enter the expression : ";
cin >> expr;
if (BalancedBraces(expr))
cout << "Braces are
balanced.";
else
cout << "Braces are not
balanced.";
return 0;
}
Please refer to the screenshot of the code to understand the indentation of the code:
Output:
1.
2.
For any doubts or questions comment below.
Get Answers For Free
Most questions answered within 1 hours.