-
Notifications
You must be signed in to change notification settings - Fork 64
/
Parenthesis Checker
79 lines (72 loc) · 989 Bytes
/
Parenthesis Checker
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
#include<stdio.h>
#include<process.h>
#define SIZE 20
int is_empty(int top)
{
if(top==-1)
return 1;
return 0;
}
int is_full(int top)
{
if(top==SIZE-1)
return 1;
return 0;
}
void push(char stack[SIZE],int *top, char x)
{
if(is_full(*top))
{
printf("Stack is full");
return;
}
else
{
stack[++(*top)]=x;
}
}
char pop(char stack[SIZE],int *top)
{ char p;
if(is_empty(*top))
{
return -999;
}
else
{
p=stack[(*top)--];
return p;
}
}
void paren_checker(char str[20],char stack[SIZE],int *top)
{
int flag=0,i,p,x;
for(i=0;str[i]!='\0';i++)
{
if(str[i]=='(')
{
x=str[i];
push(stack,top,x);
}
else
{
p=pop(stack,top);
if(p!='(')
{
flag=1;
break;
}
}
}
if(flag==0 && *top==-1)
printf("VALID");
else
printf("invalid");
}
int main()
{
char str[20],stack[SIZE];
int n,top=-1;
printf("Enter the string:");
scanf("%s",str);
paren_checker(str,stack,&top);
}