# C Programming – HackerRank Solution | INFIX TO POSTFIX CONVERSION |

### (HackerRank) Write a Modular C Programming code to solve INFIX TO POSTFIX CONVERSION, Help Swaraj to write an modular c program to based on given algorithm.

Swaraj wants to learn how to make use of stacks data structure to convert the infix expression to postfix expression. He was discussing with his class teacher. The class teacher provided him an agorithm on the conversion process.

Help Swaraj to write a modular c program to based on given algorithm.

The algorithm is as follows:

Input Format

Infix expression.

Constraints

Expression may or may not have parantheses.

Output Format

Converted postfix expression.

Sample Input 0

a+b

Sample Output 0

Given Infix Expression: a+b
Postfix Expression: ab+

Sample Input 1

(2+3*5)-1*3

Sample Output 1

Given Infix Expression: (2+3*5)-1*3
Postfix Expression: 235*+13*-

Sample Input 2

a*b+c/d

Sample Output 2

Given Infix Expression: a*b+c/d
Postfix Expression: ab*cd/+

CODE:

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

#define SIZE 100

struct stack
{
char data[SIZE];
int top;
};
void push(struct stack *sptr, char num);
char pop(struct stack *sptr);
void postfix_conv(char infix[SIZE]);
int pre(char ch);
int main()
{
char infix[50];
scanf("%s",infix);
postfix_conv(infix);
return 0;
}

void push(struct stack *sptr, char num)
{
if(sptr->top==SIZE-1)
{
printf("Stack Overflow\n");
}
else
{
sptr->top++;
sptr->data[sptr->top]=num;
}
}
char pop(struct stack *sptr)
{
char num;
num=sptr->data[sptr->top];
sptr->top--;
return num;
}
char peek(struct stack *sptr)
{
char c;
c=sptr->data[sptr->top];
return c;
}
void postfix_conv(char infix[SIZE])
{
struct stack s, * sptr;
sptr=&s;
sptr->top=-1;
char postfix[SIZE],ch,elem;
int i=0,k=0;
push(sptr,'#');
while(infix[i]!='\0')
{
ch=infix[i];
if(ch=='(')
{
push(sptr,ch);
}
else if(isalnum(ch))
{
postfix[k++]=ch;
}
else if(ch==')')
{
while(sptr->data[sptr->top]!='(')
{
postfix[k++]=pop(sptr);
}
elem=pop(sptr);
}
else
{
while(pre(peek(sptr))>=pre(ch))
{
postfix[k++]=pop(sptr);
}
push(sptr,ch);
}
i++;
}
while(sptr->data[sptr->top]!='#')
{
postfix[k++]=pop(sptr);
}
postfix[k]='\0';
printf("Given Infix Expression: %s\nPostfix Expression: %s",infix,postfix);
}

int pre(char ch)
{
switch(ch)
{
case '#':return 0;
break;
case '(':return 1;
break;
case '+':
case '-':return 2;
break;
case '/':
case '*':
case '%':return 3;
break;
case '^':return 4;
break;
}
return 0;
}

OUTPUT

Congratulations, you passed the sample test case.

Click the Submit Code button to run your code against all the test cases.

Input (stdin)

a+b

Given Infix Expression: a+b
Postfix Expression: ab+
Expected Output

Given Infix Expression: a+b
Postfix Expression: ab+

Please find some more codes of Loops, Condition Statements, 1D Arrays, 2D Arrays, Strings, Pointers, Data Structures, Files, Linked lists, Stacks, Queues, Binary Trees, MISC, Solved model question papers & Hacker Rank all solutions on the below page:

Top 100+ C Programming codes – KLE Technological University