Transform the Expression : Infix to postfix

char stack[401];
int index=-1;
void push(char value);
main()
{
char arr[401];//remember this you always forget : 1 extra for ‘\0’
char out[401];

int i,t=0,k=0;
scanf("%d",&t);

while(t--)
{
    k=0;
    i=0;
    scanf("%s",arr);
    while(arr[i]!='\0')
    {
        if(arr[i]=='('){push(arr[i]);}
        if(arr[i]=='*'){push(arr[i]);}
        if(arr[i]=='-'){push(arr[i]);}
        if(arr[i]=='+'){push(arr[i]);}
        if(arr[i]=='/'){push(arr[i]);}
        if(arr[i]=='^'){push(arr[i]);}

        if(arr[i]>='a'&& arr[i]<='z' )
        {
            out[k]=arr[i];
            k++;
        }

        if(arr[i] == ')')
        {
            while(stack[index]!='(')
            {
                out[k]= pop();
                k++;
            }
            pop();
        }
        i++;
    }
    for(i=0;i<k;i++)
    {
          printf("%c",out[i]);
    }

}

}

void push(char value)
{
index++;
stack[index] = value;

}

pop()
{
index–;
return(stack[index+1]);
}


guys i followed strict rule of parenthesis as no priorotization of operators needed here … guys can you plz help me out here if i am missing any case here it would be really helpfull…
When i submit i get Wrong answer but i got correct for test cases and some of my expressions also worked fine …

have a look at this code

for the input (a+b)*(c+d) your code gives ab+cd+ , but it should output ab+cd+**

link to code

char stack[401];
int index=-1;
void push(char value);
main()
{
char arr[401];//remember this you always forget : 1 extra for ‘\0’
char out[401];

int i,t=0,k=0;
scanf("%d\n",&t);

while(t--)
{
    k=0;
    i=0;
    scanf("%s",arr);
    while(arr[i]!='\0')
    {
        if(arr[i]=='('){push(arr[i]);}
        if(arr[i]=='*'){push(arr[i]);}
        if(arr[i]=='-'){push(arr[i]);}
        if(arr[i]=='+'){push(arr[i]);}
        if(arr[i]=='/'){push(arr[i]);}
        if(arr[i]=='^'){push(arr[i]);}

        if((arr[i]>='a'&& arr[i]<='z')|| (arr[i]>='A'&& arr[i]<='Z') )
        {
            out[k]=arr[i];
            k++;
        }

        if(arr[i] == ')')
        {
            while(stack[index]!='(')
            {
                out[k]= pop();
                k++;
            }
            pop();
        }
        i++;
    }
    if(index!=-1)
    {
        out[k]= pop();
                k++;
    }
    for(i=0;i<k;i++)
    {
          printf("%c",out[i]);
    }

}

}

void push(char value)
{
index++;
stack[index] = value;

}

pop()
{
index–;
return(stack[index+1]);
}


sorry my bad i forgot to empty the stack if it was still occupied but still it is showing wrong answer

have a look at this , debug it properly before asking

Thank u and yeah i got it

  • i did’nt empty the stack at last
  • silly mistake : missed new line character after every test case
    Happy coding !!!

all the best , happpy code