Can anybody help me to convert a infix expression into a postfix expression which deals with all three brackets i.e. ({[ in c language?

This is the pseudo code. Create a function that takes an infix expression as input and converts it to a postfix expression. It should  

take pointers to two strings as input; one containing the infix expression, and second to which this

function will write the postfix expression i.e. the declaration should be

void infixToPostfix(char *infix, char *postfix, int str_size);

The algorithm is as follows:

Iterate over the infix expression’s string. For each character c,

• If c is an operand, concatenate it with the postfix expression.

• If c is an operator, check if top of stack, say m, is an operator. If so, check if the operator

precedence of m is higher or equal to the operator precedence of c; if yes, concatenate m with

postfix expression, and pop it from the stack. Keep doing this until the top of stack is not an

operator with operator precedence higher or equal to the operator precedence of c. Then push

c on the stack.

• After completely processing input expression, TopAndPop remaining operators from stack and

concatenate each with postfix expression, in the order they are popped.

You can perform string concatenation using strcat().

Asked by:- MehreenSaleem
: 1434 At:- 5/17/2021 12:58:37 AM
C C++

1 Answers
profileImage Answered by:- vikas_jk

Brackets other than "(" are all invalid in infix to postfix conversion, continue with your conversion as shown in the below article

Infix to postfix conversion program in C using stack (With algorithm)

here is the C code for conversion from infix to postfix

#include <stdio.h>
#include <ctype.h>

#define SIZE 50            

char stack[SIZE];
int top=-1;       /* Global declarations */
push(char elem)
{                       /* Function for PUSH operation */
char pop()
{                      /* Function for POP operation */
int pr(char symbol)
{                 /* Function for precedence */
	if(symbol == '^')/* exponent operator, highest precedence*/
	else if(symbol == '*' || symbol == '/')
	else if(symbol == '+' || symbol == '-')          /* lowest precedence */
  /* Main Program */
void main()
    char infix[50],postfix[50],ch,elem;
    int i=0,k=0;
    printf("Enter Infix Expression : ");
    while( (ch=infix[i++]) != '\0')
        if( ch == '(') push(ch);
            if(isalnum(ch)) postfix[k++]=ch;
                if( ch == ')')
                    while( stack[top] != '(')
                    elem=pop(); /* Remove ( */
                {       /* Operator */
                    while( pr(stack[top]) >= pr(ch) )
    while( stack[top] != '#')     /* Pop from stack till empty */
    postfix[k]='\0';          /* Make postfix as valid string */
    printf("\nPostfix Expression =  %s\n",postfix);

You will have to modify function name, as your questions description suggests.

If the above doesn't help you can also

You can see it is mentione in the above link that "(A+B+(C+D)) is valid. {A+B+(C+D)} is invalid."

At:- 5/17/2021 8:16:38 AM

Login/Register to answer
Register directly by posting answer/details

Full Name *

Email *

By posting your answer you agree on privacy policy & terms of use