44 lines
1.1 KiB
C
44 lines
1.1 KiB
C
#include <stdio.h>
|
|
#include <string.h>
|
|
|
|
int match(char open, char close) {
|
|
return (open == '{' && close == '}') || (open == '[' && close == ']') ||
|
|
(open == '(' && close == ')');
|
|
}
|
|
|
|
void check_brackets(const char *code) {
|
|
char stack[100];
|
|
int top = -1;
|
|
|
|
printf("Read: %s\n", code);
|
|
|
|
for (int i = 0; code[i] != '\0'; i++) {
|
|
char c = code[i];
|
|
if (c == '{' || c == '[' || c == '(') {
|
|
stack[++top] = c;
|
|
} else if (c == '}' || c == ']' || c == ')') {
|
|
if (top == -1) {
|
|
printf("Unexpected closing bracket %c at position %d\n", c, i);
|
|
return;
|
|
}
|
|
if (!match(stack[top], c)) {
|
|
printf("Crossed bracket %c in %d, expected %c\n", c, i, stack[top]);
|
|
return;
|
|
}
|
|
top--;
|
|
}
|
|
}
|
|
if (top == -1) {
|
|
printf("All brackets OK");
|
|
} else {
|
|
printf("Unmatched opening bracket %c\n", stack[top]);
|
|
}
|
|
}
|
|
|
|
int main() {
|
|
char code[100];
|
|
fgets(code, 100, stdin);
|
|
check_brackets(code);
|
|
return 0;
|
|
}
|