I have provided the code I have written. All I need you to do is to rewrite the code. I need you to rewrite the comments, rename all the variables, and shift the code. It should look different once you have edited it. I would like to see another format for this code. It is in C-Programming. I think it shouldn’t take so long. Take your time, please! I really appreciate any help you can provide! CODE STARTS HERE: #include #include #include void push(char stack[], char elem, int *stack_top) { stack[*stack_top] = elem; // Changing by reference, so as to change the // original variable, not the formal argument. *stack_top = *stack_top + 1; } char peek_stack(char stack[], int *stack_top) { // Looking at the bracket at the top // of the stack, without popping it. return stack[*stack_top - 1]; } char pop(char stack[], int *stack_top) { if (*stack_top == 0) // Stack is empty return '\0'; *stack_top = *stack_top - 1; char popped_char = stack[*stack_top]; return popped_char; } int check_counter_brace(char curr_elem, char popped_elem) { /** * This function checks if the current brace is * the closing brace for the stack-top. */ if ((curr_elem == ')' && popped_elem == '(') || (curr_elem == '}' && popped_elem == '{') || (curr_elem == ']' && popped_elem == '[')) return 1; // The braces aren't counter of each other. // It is something like (], {) etc. return 0; } char get_counter_brace(char brace) { /** * This function returns the counter brace * that should have been present, wrt the * current stack top in order to have a * balanced string. */ if (brace == '(') return ')'; if (brace == '{') return '}'; if (brace == '[') return ']'; printf("\nError in input string"); exit(0); } int check_balance(const char *str) { /** * Objective - Check if the parentheses are balanced or not. * Example - [(]) is not balanced, but [()] is. * The requirement is to check the balance, and in case of * imbalance follow 2 strategies to report the issue to the user - * * 1. If each bracket opened isn't followed by a valid closing. * Example - [(] has an opening for (, but no closing, hence * report an error. Let's call it Type-I error. * * 2. When each bracket opened isn't closed, then we are to output * the serial string which must have been present in order for the * string to be balanced. Let's call it Type-II error. */ int size = strlen(str); char stack[size]; int stack_top = 0; int *stack_top_ptr = &stack_top; int idx = 0; while(str[idx] != '\0') { char curr_elem = str[idx]; if (curr_elem == '(' || curr_elem == '{' || curr_elem == '[') // Push into stack, if it is an opening brace. push(stack, curr_elem, stack_top_ptr); else if (curr_elem == ')' || curr_elem == '}' || curr_elem == ']') { // Getting the brace that is currently at the top. char popped_brace = peek_stack(stack, stack_top_ptr); if (!check_counter_brace(curr_elem, popped_brace)) { // Type-I error, so printing the index, // and the encountered brace printf("%d: %c", idx, curr_elem); return EXIT_FAILURE; } pop(stack, stack_top_ptr); } idx++; } if (stack_top == 0) { // Entire string is proceesed, and it is balanced, // as the stack is empty. return EXIT_SUCCESS; } while (stack_top != 0) { // Stack is not empty. Type-II error, // so printing the reverse string expected. char stack_top_brace = pop(stack, stack_top_ptr); printf("%c", get_counter_brace(stack_top_brace)); } // The string was processed entirely, but there were // Type-II imbalances, so we return failure. return EXIT_FAILURE; } int main(int argc, char **argv) { char *str_to_be_checked = argv[1]; int status = check_balance(str_to_be_checked); return status; }
3. I have provided the code I have written. All I need you to do is to rewrite the code. I need you to rewrite the comments, rename all the variables, and shift the code. It should look different once you have edited it. I would like to see another format for this code. It is in C-Programming. I think it shouldn’t take so long. Take your time, please! I really appreciate any help you can provide!
CODE STARTS HERE:
#include<stdio.h>
#include<strings.h>
#include<stdlib.h>
void push(char stack[], char elem, int *stack_top) {
stack[*stack_top] = elem;
// Changing by reference, so as to change the
// original variable, not the formal argument.
*stack_top = *stack_top + 1;
}
char peek_stack(char stack[], int *stack_top) {
// Looking at the bracket at the top
// of the stack, without popping it.
return stack[*stack_top - 1];
}
char pop(char stack[], int *stack_top) {
if (*stack_top == 0)
// Stack is empty
return '\0';
*stack_top = *stack_top - 1;
char popped_char = stack[*stack_top];
return popped_char;
}
int check_counter_brace(char curr_elem, char popped_elem) {
/**
* This function checks if the current brace is
* the closing brace for the stack-top.
*/
if ((curr_elem == ')' && popped_elem == '(') ||
(curr_elem == '}' && popped_elem == '{') ||
(curr_elem == ']' && popped_elem == '['))
return 1;
// The braces aren't counter of each other.
// It is something like (], {) etc.
return 0;
}
char get_counter_brace(char brace) {
/**
* This function returns the counter brace
* that should have been present, wrt the
* current stack top in order to have a
* balanced string.
*/
if (brace == '(') return ')';
if (brace == '{') return '}';
if (brace == '[') return ']';
printf("\nError in input string");
exit(0);
}
int check_balance(const char *str) {
/**
* Objective - Check if the parentheses are balanced or not.
* Example - [(]) is not balanced, but [()] is.
* The requirement is to check the balance, and in case of
* imbalance follow 2 strategies to report the issue to the user -
*
* 1. If each bracket opened isn't followed by a valid closing.
* Example - [(] has an opening for (, but no closing, hence
* report an error. Let's call it Type-I error.
*
* 2. When each bracket opened isn't closed, then we are to output
* the serial string which must have been present in order for the
* string to be balanced. Let's call it Type-II error.
*/
int size = strlen(str);
char stack[size];
int stack_top = 0;
int *stack_top_ptr = &stack_top;
int idx = 0;
while(str[idx] != '\0') {
char curr_elem = str[idx];
if (curr_elem == '(' || curr_elem == '{' || curr_elem == '[')
// Push into stack, if it is an opening brace.
push(stack, curr_elem, stack_top_ptr);
else if (curr_elem == ')' || curr_elem == '}' || curr_elem == ']') {
// Getting the brace that is currently at the top.
char popped_brace = peek_stack(stack, stack_top_ptr);
if (!check_counter_brace(curr_elem, popped_brace)) {
// Type-I error, so printing the index,
// and the encountered brace
printf("%d: %c", idx, curr_elem);
return EXIT_FAILURE;
}
pop(stack, stack_top_ptr);
}
idx++;
}
if (stack_top == 0) {
// Entire string is proceesed, and it is balanced,
// as the stack is empty.
return EXIT_SUCCESS;
}
while (stack_top != 0) {
// Stack is not empty. Type-II error,
// so printing the reverse string expected.
char stack_top_brace = pop(stack, stack_top_ptr);
printf("%c", get_counter_brace(stack_top_brace));
}
// The string was processed entirely, but there were
// Type-II imbalances, so we return failure.
return EXIT_FAILURE;
}
int main(int argc, char **argv) {
char *str_to_be_checked = argv[1];
int status = check_balance(str_to_be_checked);
return status;
}
Trending now
This is a popular solution!
Step by step
Solved in 2 steps