Hey all, Ive been working on this program for a while and keep changing it around. Im trying to have it verify that the user inputted braces are properly nested. I keep coming up with the outcome that they are not, even if they are. any input on where im going wrong is appreciated.
Code:
#include<stdbool.h>
#include<stdio.h>
#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:\n" );
while ( ( i < STACK_SIZE ) && ( ( ch = getchar() ) != '\n' ) ) {
contents[i++] = ch;
if(('('==ch)||('{'==ch))
{push(ch);
fst = ch;
}
else if((')'==ch)||('}'==ch))
{
pop(contents);
lst = ch;
}
}
if ( fst == lst )
printf("\nParenthesis/brackets nested properly....");
else printf("\nParenthesis/brackets not nested properly...");
return 0;
}