-
Notifications
You must be signed in to change notification settings - Fork 231
Expand file tree
/
Copy pathparanthesisChecker.c
More file actions
56 lines (51 loc) · 811 Bytes
/
paranthesisChecker.c
File metadata and controls
56 lines (51 loc) · 811 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
#include<stdio.h>
#include<conio.h>
#include<string.h>
int top=-1;
char stack[10];
void push(char ch){
if(top==9)
printf("Stack is full");
else
stack[++top]=ch;
}
char pop(){
char c1;
if(top==-1)
printf("stack is empty");
else
c1=stack[top--];
return c1;
}
//Paranthesis checker using Stack
void main(){
char exp[10],tp;
int i,flag=1;
clrscr();
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)
flag=0;
else
tp=pop();
}
if(exp[i]==')'&&(tp=='{'||tp=='['))
flag=0;
if(exp[i]=='}'&&(tp=='('||tp=='['))
flag=0;
if(exp[i]==']'&&(tp=='{'||tp=='('))
flag=0;
}
if(top>=0)
flag=0;
if(flag==1)
printf("match");
else
printf("Mismatch");
getch();
}