We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2
#include <stdio.
h> #include <stdbool.h> #include <string.h> #define MAX 10
char st[MAX]; int top = -1; void push(char st[], char val); char pop(char st[]); void Balance(char source[]);
void push(char st[], char val)
{ printf("Attempting to push '%c' onto the stack\n", val); if (top == MAX - 1) printf("Stack Overflow\n"); else { top++; st[top] = val; printf("Pushed '%c' onto the stack. New top: %d\n", val, top); } }
char pop(char st[])
{ printf("Attempting to pop from the stack\n"); char val = ' '; if (top == -1) printf("Stack Underflow\n"); else { val = st[top]; top--; printf("Popped '%c' from the stack. New top: %d\n", val, top); } return val; }
void Balance(char source[])
{ printf("Starting to check balance for: %s\n", source); int i = 0; while (source[i] != '\0') { printf("Checking character: %c\n", source[i]); if (source[i] == '(' || source[i] == '{' || source[i] == '[') { printf("Found opening bracket: %c\n", source[i]); push(st, source[i]); } else if (source[i] == ')' && st[top] == '(' || source[i] == '}' && st[top] == '{' || source[i] == ']' && st[top] == '[') { pop(st); } i++; } if (top == -1) printf("The expression is balanced\n"); else printf("The expression is not balanced\n"); }