Dip Chakraborty
Dip Chakraborty

Reputation: 67

stack problem: want to check if expression is correctly parenthesized and balancing and matching

#include<stdio.h>
#include<string.h>

int stack[20];
int top=-1;
void push(int x)
{
    stack[top]=x;
    top++;
}

int pop()
{
    return stack[top--];
}

int checkmatching(char a[])
{
    int i=0,j=top;
    int count = top;
    while(i+1<=count)
    {
        if(strcmp(a[i],a[j])==0)
        {
            pop(a[i]);
            pop(a[j]);
            i++;
            j--;
        }
        else return -1;
    }
   return 1;
}

int main()
{
    char exp[20],i,m;
    printf("give the expression: ");
    scanf("%s", &exp);
    for(i=0;exp[i]!='\0';i++)
    {
        push(exp[i]);
    }
    m=checkmatching(exp);
    if(m>0)
        printf("matched");
    else printf("no match");
  return 0;
}

I have a warning for this code:

|21|warning: passing argument 1 of 'strcmp' makes pointer from integer without a cast |43|note: expected 'const char *' but argument is of type 'char' |21|warning: passing argument 2 of 'strcmp' makes pointer from integer without a cast| |43|note: expected 'const char *' but argument is of type 'char'|

Upvotes: 2

Views: 214

Answers (3)

Sandipan
Sandipan

Reputation: 703

Don't use strcmp(), use if().

If(a[i] == a[j])
{
     pop(a[i]);
     pop(a[j]);
     i++;
     j--;
}

Upvotes: 1

A. Guy
A. Guy

Reputation: 702

strcmp() compares strings, and you're passing it chars (notice that a is a string) you can compare chars with regular ==

from a brief look you're also trying to use pop with an argument, and I'm not sure what are you trying to achieve.

are you trying to check if the expiration is a palindrome? why using a stack for that? clear your question please

Upvotes: 1

Mike
Mike

Reputation: 4288

You are comparing two char with strcmp (string compare)

Just compare the chars directly:

int checkmatching(char a[])
{
    int i=0,j=top;
    int count = top;
    while(i+1<=count)
    {
        if((a[i] == a[j]))
        {
            pop(a[i]);
            pop(a[j]);
            i++;
            j--;
        }
        else return -1;
    }
   return 1;
}

Upvotes: 1

Related Questions