#include #include #define MAX_LEN 100 int is_matching_pair(char open, char close) { return (open == '(' && close == ')') || (open == '{' && close == '}') || (open == '[' && close == ']') || (open == '<' && close == '>'); } char matching_close(char open) { return open == '(' ? ')' : open == '{' ? '}' : open == '[' ? ']' : '>'; } void check_brackets(const char *code) { char stack[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; } 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, matching_close(stack[top])); return; } top--; } } if (top != -1) { printf("Missing closing brackets: "); while (top != -1) { printf("%c", matching_close(stack[top--])); } printf("\n"); } 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; }