-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathParanthesis check
More file actions
57 lines (54 loc) · 888 Bytes
/
Paranthesis check
File metadata and controls
57 lines (54 loc) · 888 Bytes
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
// You can use stl and easily solved (c++)
#include<stdio.h>
#include<string.h>
int stack[20];
char pop()
int top=-1;
int f=0;
int main()
{
int n;
scanf("%d",&n);
char a[n];
for(int i=0;i<strlen(a);i++)
{
if(a[i]=='('||a[i]=='['||a[i]=='{')
{
push(a[i]);
}
else{
if(a[i]==')'||a[i]==']'||a[i]=='}')
{
if(top==-1)
{
f=1;
}
else{char t=pop();
if(a[i]==')'&&(t=='['||t=='{'))
f=1;
if(a[i]=='}'&&(t=='('||t=='['))
f=1;
if(a[i]==']'&&(t=='{'||t=='('))
f=1;
}
}
}
}
if(top>=0)
{
f==1;
}
if(f==1)
printf("Unbalanced\n");
else
printf("Balanced\n");
return 0;
}
void push(char a)
{
stack[++top]=a;
}
char pop()
{
return stack[top--];
}