-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathBalanced_paranthesis.cpp
61 lines (59 loc) · 1.18 KB
/
Balanced_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
57
58
59
60
61
#include <iostream>
#include <stack>
#include <string.h>
#include <bits/stdc++.h>
using namespace std;
bool isbalanced_or_not(string expr)
{
stack<char> st;
char c;
int i,l;
l=expr.length();
for(i=0;i<l;i++)
{
if(expr[i]=='{' || expr[i]=='[' || expr[i]=='(')
{
st.push(expr[i]);
continue;
}
if(st.empty())
{
return false;
}
switch (expr[i])
{
case ')':
c = st.top();
st.pop();
if (c=='{' || c=='[')
return false;
break;
case '}':
c = st.top();
st.pop();
if (c=='(' || c=='[')
return false;
break;
case ']':
c = st.top();
st.pop();
if (c =='(' || c == '{')
return false;
break;
}
}
}
int main()
{
string exps;
cin>>exps;
if(isbalanced_or_not)
{
cout<<"Brackets are balanced";
}
else
{
cout<<"Brackets are not balanced";
}
return 0;
}