-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparent_checker.c
More file actions
111 lines (97 loc) · 1.72 KB
/
Copy pathparent_checker.c
File metadata and controls
111 lines (97 loc) · 1.72 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include<stdio.h>
#include<string.h>
#define MAX 100
typedef struct{
char item[MAX];
int top;
}Stack;
Stack s;
//Creating the stack
void initialize()
{
s.top = -1;
}
//Checking if a stack is empty or not
int isEmpty()
{
if(s.top==-1)
return 1;
return 0;
}
//Pushing the character into the stack
void push(char w)
{
s.top++;
s.item[s.top] = w;
}
//Deleting the top character from the stack
void pop()
{
s.top--;
}
//Returning the top most character in the stack
int peek()
{
return s.item[s.top];
}
//Will display the stack
/* Not required for this program, but could be used to see the stack.
void show()
{
int i;
for(i=0;i<=s.top;i++)
{
printf("%c\t",s.item[i]);
}
}
*/
int main()
{
initialize();
//char word[100] = "[(])”"; == Unbalanced
//char word[100] = "[()]{}{[()()]()}"; // == Balanced
char word[100];
printf("Enter the parantheses expression:"); //Enter your own expression
scanf("%s",word);
int len = strlen(word);
int i, flag = 1;
for(i=0;i<len;i++)
{
if(word[i] == '}' || word[i] == ']' || word[i] == ')')
{
if(isEmpty())
{
flag = 0;
}
else
{
char n = peek();
if(n == '(' && word[i] == ')')
{
pop();
}
else if(n == '[' && word[i] == ']')
{
pop();
}
else if(n == '{' && word[i] == '}')
{
pop();
}
else
{
flag = 0;
}
}
}
else
{
push(word[i]);
}
}
if(isEmpty() && flag==1)
printf("\nBalanced");
else
printf("\nUnbalanced");
return 0;
}