-
Notifications
You must be signed in to change notification settings - Fork 0
/
BalancePara.c
52 lines (49 loc) · 1.07 KB
/
BalancePara.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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define N 1000
int CheckPara(char String[N], int len)
{
char Stack[N] = {0};
int top = -1;
for(int i = 0; i < len; i++) {
if(String[i] == '{' || String[i] == '[' || String[i] == '(') {
Stack[++top] = String[i];
}
if(String[i] == ']') {
if(Stack[top] != '[') {
return 0;
}
top--;
}
if(String[i] == '}') {
if(Stack[top] != '{') {
return 0;
}
top--;
}
if(String[i] == ')') {
if(Stack[top] != '(') {
return 0;
}
top--;
}
}
if(top < 0)
return 1;
else
return 0;
}
int main()
{
char String[N];
int result = 0;
printf("Enter the expression : ");
scanf(" %s", String);
result = CheckPara(String, strlen(String));
if(result) {
printf("Braces are balanced \n");
} else {
printf("Braces are not balanced \n");
}
}