paranthesis count

i am trying to implement balancing of braces by using stacks however i dont get the correct output,please help!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define STACKSIZE 100
struct stackk
{
    int top;
    int item[STACKSIZE];
};
 struct stackk s;

void push(char c)
{
    if(s.top==STACKSIZE-1)
    {
        printf("Stack overflow");
        exit(0);
    }
    s.item[++(s.top)]=c;
}
char pop()
{
    if(s.top==-1)
    {
        printf("Stack underflow");
        exit(0);
    }
    return s.item[(s.top)--];
}
int empty()
{
    if(s.top==-1)
        return 1;
    else return 0;
}
int main()
{
    s.top=-1;
    int valid=1;
    char str[100],c,p;
    int len,i;
    printf("Enter string\n");
    scanf("%s",str);
    len=strlen(str);
    for(i=0;i<len;i++)
    {
        c=str[i];
        if(c=='{')
            push(c);
        if(c=='}')
        {
            if(empty)
                valid=0;
            else
            {
               p=pop();
               if(p!='{')
                    valid=0;
            }
        }
    }
    if(!empty())
        valid=0;
    if(valid)
        printf("string is valid");
    else
        printf("not valid");
}
One obvious problem:
Line 52: You're attempting to call empty, but don;t include the () to indicate a function call. You're going to get the address of the function instead, which will always be non-zero (true).

Other than that, your program seems to work for the few trivial examples I tried.
thnx alot:)
Topic archived. No new replies allowed.