forked from maurya27/Hacktoberfest2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbalancedparenthesis.cpp
69 lines (49 loc) · 1.17 KB
/
balancedparenthesis.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
62
63
64
65
66
67
68
69
// Header Files and namespaces
#include<bits/stdc++.h>
using namespace std;
bool isbalancedparanthesis(char *a){
stack <char> s;
for (int i = 0 ; a[i]!=0 ; i++){
char ch = a[i];
switch (ch){
case '(':
case '{':
case '[':
s.push(ch);
break;
case ')':
if (s.empty() or s.top() != '(' ){
return false;
}
s.pop();
break;
case '}':
if (s.empty() or s.top() != '{' ){
return false;
}
s.pop();
break;
case ']':
if (s.empty() or s.top() != '[' ){
return false;
}
s.pop();
}
}
if (s.empty()) return true;
else return false;
}
// Code
int main()
{
char *a = new char;
cout<<"enter an equation to check is its balanced or not: ";
cin>>a;
if (isbalancedparanthesis(a)){
cout<<"Balanced";
}
else{
cout<<"Not balanced";
}
return 0;
}