Skip to content

Commit f6b5354

Browse files
authored
Balanced_Parenthesis_check
Added a C code for balanced parenthesis check
1 parent 09f9f80 commit f6b5354

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#include<stdio.h>
2+
void push(char *s,int*,char);
3+
char pop(char* s, int*);
4+
int isempty(int);
5+
int match(char *);
6+
int main()
7+
{
8+
char expr[100];
9+
printf("Enter the expression\n");
10+
scanf("%s",expr);
11+
int result=match(expr);
12+
if(result)
13+
printf("Matching succeeds\n");
14+
else
15+
printf("matching fails\n");
16+
}
17+
18+
int match( char *expr)
19+
{
20+
char ch,x,s[100];
21+
int top,i;
22+
top=-1;
23+
i=0;
24+
while(expr[i]!='\0')
25+
{
26+
ch=expr[i];
27+
switch(ch)
28+
{
29+
case '(':
30+
case '{':
31+
case '[': push(s,&top,ch);
32+
break;
33+
case ')':if(!isempty(top))
34+
{
35+
x=pop(s,&top);
36+
if(x!='(')
37+
return 0;
38+
break;
39+
}
40+
else
41+
return 0;
42+
43+
case '}':if(!isempty(top))
44+
{
45+
x=pop(s,&top);
46+
if(x!='{')
47+
return 0;
48+
break;
49+
}
50+
else
51+
return 0;
52+
53+
case ']':if(!isempty(top))
54+
{
55+
x=pop(s,&top);
56+
if(x!='[')
57+
return 0;
58+
break;
59+
}
60+
else
61+
return 0;
62+
}
63+
i++;
64+
}
65+
if(isempty(top))
66+
return 1;
67+
return 0;
68+
}
69+
70+
71+
void push(char *s,int *t,char x)
72+
{
73+
++*t;
74+
s[*t]=x;
75+
}
76+
77+
char pop(char *s, int *t)
78+
{
79+
char x;
80+
x=s[*t];
81+
--*t;
82+
return x;
83+
}
84+
85+
int isempty(int top)
86+
{
87+
if(top==-1)
88+
return 1;
89+
return 0;
90+
}

0 commit comments

Comments
 (0)