2024-10-24 10:20:55 +00:00
|
|
|
#include <stdio.h>
|
2024-10-24 12:23:37 +00:00
|
|
|
#include <stdlib.h>
|
2024-10-24 10:20:55 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2024-10-24 12:23:37 +00:00
|
|
|
#define MAX_LENGTH 100
|
2024-10-24 10:20:55 +00:00
|
|
|
|
2024-10-31 20:40:32 +00:00
|
|
|
// Function to check if two brackets are matching
|
2024-10-24 12:23:37 +00:00
|
|
|
int isMatching(char opening, char closing) {
|
|
|
|
return (opening == '{' && closing == '}') ||
|
|
|
|
(opening == '[' && closing == ']') ||
|
|
|
|
(opening == '(' && closing == ')') ||
|
|
|
|
(opening == '<' && closing == '>');
|
2024-10-24 10:20:55 +00:00
|
|
|
}
|
|
|
|
|
2024-10-31 20:46:10 +00:00
|
|
|
// Function to get the expected closing bracket for a given opening bracket
|
|
|
|
char getExpectedClosingBracket(char opening) {
|
|
|
|
return (opening == '{') ? '}' :
|
|
|
|
(opening == '[') ? ']' :
|
|
|
|
(opening == '(') ? ')' :
|
|
|
|
(opening == '<') ? '>' : '\0';
|
|
|
|
}
|
|
|
|
|
2024-10-24 10:20:55 +00:00
|
|
|
int main() {
|
2024-10-24 12:23:37 +00:00
|
|
|
char input[MAX_LENGTH + 1];
|
|
|
|
char stack[MAX_LENGTH];
|
2024-10-31 20:40:32 +00:00
|
|
|
int top = -1; // Stack pointer
|
|
|
|
int foundBracket = 0; // Flag to check if any bracket is found
|
2024-10-24 12:23:37 +00:00
|
|
|
|
2024-10-31 20:40:32 +00:00
|
|
|
// Reading the string
|
2024-10-24 12:23:37 +00:00
|
|
|
fgets(input, sizeof(input), stdin);
|
|
|
|
|
2024-10-31 20:40:32 +00:00
|
|
|
// Remove newline character if present
|
2024-10-24 12:23:37 +00:00
|
|
|
input[strcspn(input, "\n")] = 0;
|
|
|
|
|
2024-10-31 20:40:32 +00:00
|
|
|
// Check if string length exceeds MAX_LENGTH
|
2024-10-24 12:23:37 +00:00
|
|
|
if (strlen(input) > MAX_LENGTH) {
|
2024-10-31 20:52:38 +00:00
|
|
|
printf("Error: Input length exceeds 100 characters.\n");
|
2024-10-24 12:23:37 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2024-10-24 10:20:55 +00:00
|
|
|
|
2024-10-31 20:52:38 +00:00
|
|
|
// Print the input line for the output format
|
|
|
|
printf("Read: %s\n", input);
|
|
|
|
|
2024-10-31 20:40:32 +00:00
|
|
|
// Iterating over each character in the string
|
2024-10-24 12:23:37 +00:00
|
|
|
for (int i = 0; i < strlen(input); i++) {
|
|
|
|
char current = input[i];
|
2024-10-24 10:20:55 +00:00
|
|
|
|
2024-10-31 20:40:32 +00:00
|
|
|
// Check if current character is an opening bracket
|
2024-10-24 12:23:37 +00:00
|
|
|
if (current == '{' || current == '[' || current == '(' || current == '<') {
|
2024-10-31 20:40:32 +00:00
|
|
|
stack[++top] = current; // Push to stack
|
|
|
|
foundBracket = 1; // Found at least one bracket
|
2024-10-24 12:23:37 +00:00
|
|
|
}
|
2024-10-31 20:40:32 +00:00
|
|
|
// If current character is a closing bracket
|
2024-10-24 12:23:37 +00:00
|
|
|
else if (current == '}' || current == ']' || current == ')' || current == '>') {
|
2024-10-31 20:40:32 +00:00
|
|
|
// If stack is empty, it's an error
|
2024-10-24 10:28:31 +00:00
|
|
|
if (top == -1) {
|
2024-10-31 20:52:38 +00:00
|
|
|
printf("Unexpected closing bracket %c in %d\n", current, i);
|
2024-10-31 20:40:32 +00:00
|
|
|
return 0;
|
2024-10-24 10:28:31 +00:00
|
|
|
}
|
|
|
|
|
2024-10-31 20:40:32 +00:00
|
|
|
// If brackets do not match, it's an error
|
2024-10-24 12:23:37 +00:00
|
|
|
if (!isMatching(stack[top--], current)) {
|
2024-10-31 20:52:38 +00:00
|
|
|
printf("Crossed bracket %c in %d, expected %c.\n", current, i,
|
2024-10-31 20:46:10 +00:00
|
|
|
getExpectedClosingBracket(stack[top + 1]));
|
2024-10-31 20:40:32 +00:00
|
|
|
return 0;
|
2024-10-24 10:20:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-31 20:40:32 +00:00
|
|
|
// If there are unmatched opening brackets left in the stack
|
2024-10-24 10:20:55 +00:00
|
|
|
if (top != -1) {
|
2024-10-31 20:54:31 +00:00
|
|
|
printf("Missing closing brackets: ");
|
|
|
|
for (int i = top; i >= 0; i--) {
|
|
|
|
printf("%c", getExpectedClosingBracket(stack[i]));
|
|
|
|
}
|
|
|
|
printf("\n");
|
2024-10-31 20:40:32 +00:00
|
|
|
return 0;
|
2024-10-24 10:20:55 +00:00
|
|
|
}
|
|
|
|
|
2024-10-31 20:40:32 +00:00
|
|
|
// If no brackets are found, output a success message
|
2024-10-24 12:40:07 +00:00
|
|
|
if (!foundBracket) {
|
2024-10-24 12:49:24 +00:00
|
|
|
printf("All brackets OK\n");
|
2024-10-24 12:40:07 +00:00
|
|
|
} else {
|
2024-10-31 20:40:32 +00:00
|
|
|
// Expected correct output if all brackets match
|
2024-10-24 12:44:58 +00:00
|
|
|
printf("All brackets OK\n");
|
2024-10-24 12:40:07 +00:00
|
|
|
}
|
|
|
|
|
2024-10-24 12:23:37 +00:00
|
|
|
return 0;
|
2024-10-24 10:20:55 +00:00
|
|
|
}
|