forked from CodeMacrocosm/Algo-a-Thon-22
-
Notifications
You must be signed in to change notification settings - Fork 0
/
balance paranthesis.cpp
56 lines (55 loc) 路 1.1 KB
/
balance paranthesis.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <bits/stdc++.h>
using namespace std;
void balance(string s)
{
stack<char> st;
int l=s.length();
int i,flag=0;
for(i=0;i<l;i++)
{
if((s[i]=='(')||(s[i]=='{')||(s[i]=='['))
st.push(s[i]);
else if(s[i]==')')
{
if((!st.empty())&&(st.top()=='('))
st.pop();
else
{
flag=1;
break;
}
}
else if(s[i]=='}')
{
if((!st.empty())&&(st.top()=='{'))
st.pop();
else
{
flag=1;
break;
}
}
else if(s[i]==']')
{
if((!st.empty())&&(st.top()=='['))
st.pop();
else
{
flag=1;
break;
}
}
}
if((flag==1)||(!st.empty()))
cout<<"Unbalanced paranthesis"<<endl;
else
cout<<"Balanced paranthesis"<<endl;
}
int main()
{
char exp[100];
cout<<"Enter the expression:";
cin>>exp;
balance(exp);
return 0;
}