-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathBalance_Bracket.c
64 lines (63 loc) · 1.2 KB
/
Balance_Bracket.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
56
57
58
59
60
61
62
63
64
#include <stdio.h>
#define n 1000000
char s[n];
int top = -1;
void push(char);
void pop();
int size();
int main()
{
int t, i;
printf("For How Many Brackets You to Check?\n");
scanf("%d", &t);
while (t--)
{
char str[1000000];
printf("Enter Brackets: ");
scanf("%s", str);
for (i = 0; str[i] != '\0'; i++)
{
if (str[i] == '{' || str[i] == '[' || str[i] == '(')
{
push(str[i]);
}
else
{
if ((str[i] == '}' && s[top] == '{') || (str[i] == ']' && s[top] == '[') || (str[i] == ')' && s[top] == '('))
{
pop();
}
}
}
if (size() == 0)
{
top = -1;
printf("Your Brackets is Balanced.\n");
}
else
{
printf("Your Brackets is NOT Balanced.\n");
}
top = -1;
}
return 0;
}
void push(char ch)
{
if (top < n - 1)
{
top++;
s[top] = ch;
}
}
void pop()
{
if (top != -1)
{
top--;
}
}
int size()
{
return top + 1;
}