Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Modify the code to decided if parentheses and/or braces are nested properly. exa

ID: 3655755 • Letter: M

Question

Modify the code to decided if parentheses and/or braces are nested properly. example - (({}{()}) - is nested properly, (({)}- is not nested properly. Hint: as the program reads characters, have it push each left parenthesis or left brace. When it reads a right parenthesis or brace, have it pop the stack and check that the item popped is a matching parenthesis or brace. if not, they aren't nested properly. when the program read a new line character have it check whether the stack is empty; if so, the parenthesis and/or braces are matched. If the stack isnt empty (or stack_underflow is ever called), the braces aren't matched. If stack_underflow is called, have the program print the message "stack overflow" and terminate. #include#include #define STACK_SIZE 20 char contents[STACK_SIZE]; int top=0; void make_empty(void) { top=0; } bool is_empty(void) { return top==0; } bool is_full(void) { return top==STACK_SIZE; } void push(char ch) { if(is_full()) stack_overflow(); else contents[top++]=ch; } int pop(void) { if(is_empty()) stack_underflow(); else return contents[--top]; } int main(void) { int ch; int count; int i = 0; printf( "Enter parentheses and/or braces: " ); return 0; }

Explanation / Answer

#include #include #define STACK_SIZE 100 char contents[STACK_SIZE]; int top = 0; void stack_overflow(void) {     printf("invalid under"); } void stack_underflow(void) {     printf("invalid over"); } void make_empty(void) {     top = 0; } bool is_empty(void) {     return top==0; } bool is_full(void) {     return top==STACK_SIZE; } char push(char ch) {     if(is_full())         stack_overflow();     else         contents[top++]=ch; } char pop(char contents[]) {          if(is_empty())         stack_underflow();     else         return contents[--top]; } int main(void) {     char ch,fst,lst;                              int i = 0;             printf( "Enter parenthesesor braces: " );     while ( ( i
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote