#include #include #define MAX_LEN 100 int is_matching_pair(char open, char close) { return (open == '(' && close == ')') || (open == '{' && close == '}') || (open == '[' && close == ']') || (open == '<' && close == '>'); } void check_brackets(const char *code) { char stack[MAX_LEN]; int positions[MAX_LEN]; int top = -1; printf("Read: %s\n", code); for (int i = 0; i < strlen(code); i++) { char ch = code[i]; if (ch == '(' || ch == '{' || ch == '[' || ch == '<') { if (top < MAX_LEN - 1) { stack[++top] = ch; positions[top] = i; } else { printf("Stack overflow at position %d\n", i); return; } } else if (ch == ')' || ch == '}' || ch == ']' || ch == '>') { if (top == -1) { printf("Unexpected closing bracket %c in %d\n", ch, i); return; } if (!is_matching_pair(stack[top], ch)) { printf("Crossed bracket %c in %d, expected %c\n", ch, i, stack[top] == '(' ? ')' : stack[top] == '{' ? '}' : stack[top] == '[' ? ']' : '>'); return; } top--; } } if (top != -1) { printf("Unmatched opening bracket %c at position %d, expected %c\n", stack[top], positions[top], stack[top] == '(' ? ')' : stack[top] == '{' ? '}' : stack[top] == '[' ? ']' : '>'); } else { printf("All brackets OK\n"); } } int main() { char code[MAX_LEN]; if (fgets(code, MAX_LEN, stdin) != NULL) { code[strcspn(code, "\n")] = '\0'; check_brackets(code); } return 0; }