Visit: https://financeclub.viden.io
Stack Balancing
#include<bits/stdc++.h> using namespace std; bool balancing(string str) { stack<char> s; char x; for(int i=0;i<str.length();i++) { if(str[i] == '(' || str[i]=='{' || str[i]=='[') { s.push(str[i]); } if(s.empty()) { return false; } switch(str[i]) { case ')': x=s.top(); s.pop(); if (x=='{' || x=='[') return false; break; case '}': // Store the top element in b x = s.top(); s.pop(); if (x=='(' || x=='[') return false; break; case ']': // Store the top element in c x = s.top(); s.pop(); if (x =='(' || x == '{') return false; break; } } // Check Empty Stack return (s.empty()); } int main() { string expr = "{()})"; if (balancing(expr)) cout << "Balanced"; else cout << "Not Balanced"; return 0; }