-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparenthsis checking.c
79 lines (76 loc) · 1.05 KB
/
parenthsis checking.c
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
70
71
72
73
74
75
76
77
78
79
#include <stdio.h>
#include <string.h>
#define MAX 20
void push(char);
char pop();
char stack[MAX];
int valid=1,top=-1;
void main()
{
char exp[MAX],temp;
int i;
printf("enter the expression=");
gets(exp);
for(i=0;i<strlen(exp);i++)
{
if (exp[i]=='('||exp[i]=='{'||exp[i]=='[')
{
push(exp[i]);
}
if (exp[i]==')'||exp[i]=='}'||exp[i]==']')
{
if (top==-1)
{
valid=0;
}
else
{
temp=pop();
if (exp[i]==')'&&(temp=='['||temp=='{'))
valid=0;
if (exp[i]==']'&&(temp=='('||temp=='{'))
valid=0;
if (exp[i]=='}'&&(temp=='['||temp=='('))
valid=0;
}
}
}
if (top>=0)
{
valid=0;
}
if (valid==1)
{
printf("Valid expression");
}
else
{
printf("Not valid expression");
}
}
void push(char ch)
{
if(top==MAX-1)
{
printf("Stack Over flow");
}
else
{
top++;
stack[top]=ch;
}
}
char pop()
{
char c;
if (top==-1)
{
printf("Stack underflow");
}
else
{
c=stack[top];
top--;
return c;
}
}