41 lines
914 B
C
41 lines
914 B
C
#include <stdio.h>
|
|
#include <string.h>
|
|
|
|
int match(char open, char close) {
|
|
return (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 == '(') {
|
|
if (top < 99) {
|
|
stack[++top] = c;
|
|
}
|
|
} else if (c == '}' || c == ')') {
|
|
if (top == -1 || !match(stack[top], c)) {
|
|
printf("Error at %d\n", i);
|
|
return;
|
|
}
|
|
top--;
|
|
}
|
|
}
|
|
if (top == -1) {
|
|
printf("All brackets OK\n");
|
|
} else {
|
|
printf("Unmatched brackets\n");
|
|
}
|
|
}
|
|
|
|
int main() {
|
|
char code[100];
|
|
printf("Enter code: ");
|
|
fgets(code, 100, stdin);
|
|
check_brackets(code);
|
|
return 0;
|
|
} |