-
Notifications
You must be signed in to change notification settings - Fork 0
/
Balancing of Symbols
98 lines (90 loc) · 1.49 KB
/
Balancing of Symbols
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
#include<stdio.h>
#include<malloc.h>
struct stack
{
char *array;
int capacity;
int top;
};
int isStackEmpty(struct stack S)
{
if(S.top==0)
return 1;
else
return 0;
}
void stackPush(struct stack *S,char element)
{
if(isStackFull(*S))
return ;
S->array[S->top]=element;
S->top++;
}
int isStackFull(struct stack S)
{
if(S.top==S.capacity)
return 1;
else
return 0;
}
char stackPop(struct stack *S)
{
if(isStackEmpty(*S))
return '\0';
S->top--;
return S->array[S->top];
}
int checkBalance(char array[])
{
int i;
struct stack S;
S.top=0;
S.capacity=10;
S.array=(char*)malloc(S.capacity*sizeof(char));
for(i=0;array[i]!='\0';i++)
{
if((array[i]=='{')||(array[i]=='[')||(array[i]=='('))
{
stackPush(&S,array[i]);
}
else if(array[i]=='}')
{
if(stackPop(&S)!='{')
return 0;
}
else if(array[i]==']')
{
if(stackPop(&S)!='[')
return 0;
}
else if(array[i]==')' )
{
if(stackPop(&S)!='(')
return 0;
}
}
if(isStackEmpty(S))
return 1;
else
return 0;
}
void main()
{
int result=0;
char expr[20];
struct stack S;
S.top=0;
S.capacity=10;
S.array=(char*)malloc(S.capacity*sizeof(char));
printf("Enter the expression:");
scanf("%s",expr);
result=checkBalance(expr);
if(result)
{
printf("\n Expression is balanced in terms of Symbols");
}
else
{
printf("\n Expression is not balanced in terms of Symbols");
}
}