-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparanthesis_checker.c
55 lines (53 loc) · 1.26 KB
/
paranthesis_checker.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
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define max 20
int stk[max], top = -1;
void push(char c){
if(top == max - 1){
printf("\nStack Overflow");
}
else{
stk[++top] = c;
}
}
int pop(){
if(top == -1){
printf("\nStack Underflow");
}
else{
return stk[top--]; // -- after top so that top value is returned first and top decremented later
}
}
int main(){
char exp[max], temp;
int i, flag = 1;
printf("\t\tParanthesis Checker\n");
printf("Enter the expression...\t\t");
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) // if stack is already empty & we did encounter closing bracket first
flag = 0; // flag 0 means mistake
else{
temp = pop();
if(temp == '{' && (exp[i] == ')' || exp[i] == ']'))
flag = 0;
if(temp == '[' && (exp[i] == ')' || exp[i] == '}'))
flag = 0;
if(temp == '(' && (exp[i] == '}' || exp[i] == ']'))
flag = 0;
}
}
}
if (top >= 0)
flag = 0;
if (flag == 0)
printf("\n\tWrong Expression");
if (flag == 1)
printf("\n\tCorrect Expression");
return 0;
}