forked from ruv1nce/42-exam_beginner
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbrackets.c
More file actions
78 lines (71 loc) · 1.14 KB
/
Copy pathbrackets.c
File metadata and controls
78 lines (71 loc) · 1.14 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
#include <unistd.h>
static int match_brackets(char a, char b)
{
if ((a == '(' && b == ')') || (a == '[' && b == ']') || (a == '{' && b == '}'))
return (1);
return (0);
}
static int count_brackets(char *s)
{
int len;
int i;
i = 0;
len = 0;
while (s[i])
{
if (s[i] == '(' || s[i] == ')' || s[i] == '[' || s[i] == ']' || s[i] == '{' || s[i] == '}')
len++;
i++;
}
return (len);
}
static int brackets(char *s, int len)
{
char br[len + 1];
int i;
int j;
i = 0;
j = 0;
br[len] = '\0';
while (s[i])
{
if (s[i] == '(' || s[i] == '[' || s[i] == '{')
{
br[j] = s[i];
j++;
}
else if (s[i] == ')' || s[i] == ']' || s[i] == '}')
{
if (!j)
return (0);
else
{
if (match_brackets(br[j - 1], s[i]))
br[--j] = '0';
else
return (0);
}
}
i++;
}
if (j == 0 && br[j] == '0')
return (1);
else
return (0);
}
int main(int argc, char **argv)
{
int i;
int len;
i = 0;
if (argc > 1)
while (++i < argc)
{
if (!(len = count_brackets(argv[i])))
write(1, "OK\n", 3);
else
brackets(argv[i], len) ? write(1, "OK\n", 3) : write(1, "Error\n", 6);
}
else
write(1, "\n", 1);
}