Learn How To Convert Prefix To Infix Notation using Stack in C Programming Language. The Prefix notation is also known as Polish Notation. Before you proceed further with this code, you must know the complete operations of stack data structure.
Prefix To Infix Conversion Example
Prefix String: +22
Infix String: 2 + 2
Algorithm To Convert Prefix Expression into Infix Notation
- The Reversed Prefix Expression is pushed on the Stack.
- If the Stack is not Empty, then Pop the elements from the Stack
- If the popped character is an Operator
- Input Opening Parantheses ‘(‘ and print it on the console.
- Display popped character from the Stack.
- Input Closing Parantheses ‘)’ and print it on the console.
- If the popped character is an empty space, then print it on the console.
- Repeat the steps till the Prefix expression is not scanned completely.
C Program For Prefix To Infix Conversion using Stack Data Structure
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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | #include<stdio.h> #include <string.h> #include <ctype.h> char operand_stack[50][80], operator_stack[50]; int top_operator = -1, top_operand = -1; int push_operator(char operator) { operator_stack[++top_operator] = operator; } int push_operand(char *opnd) { strcpy(operand_stack[++top_operand], opnd); } char pop_operator() { return(operator_stack[top_operator--]); } char *pop_operand() { return(operand_stack[top_operand--]); } int empty(int t) { if(t == 0) { return 1; } else { return 0; } } int main() { char prefix_expression[50], ch, temporary_string[50], operand_a[50], operand_b[50], operator[2]; int count = 0, k = 0, operand_count = 0; printf("\nEnter a Prefix Expression:\t"); scanf("%s", prefix_expression); while((ch = prefix_expression[count++]) != '\0') { if(isalnum(ch)) { temporary_string[0] = ch; temporary_string[1]='\0'; push_operand(temporary_string); operand_count++; if(operand_count >= 2) { strcpy(operand_b, pop_operand()); strcpy(operand_a, pop_operand()); strcpy(temporary_string, "("); strcat(temporary_string, operand_a); ch = pop_operator(); operator[0] = ch; operator[1] = '\0'; strcat(temporary_string, operator); strcat(temporary_string, operand_b); strcat(temporary_string, ")"); push_operand(temporary_string); operand_count = operand_count - 1; } } else { push_operator(ch); if(operand_count == 1) { operand_count = 0; } } } if(!empty(top_operand)) { strcpy(operand_b, pop_operand()); strcpy(operand_a, pop_operand()); strcpy(temporary_string, "("); strcat(temporary_string, operand_a); ch = pop_operator(); operator[0] = ch; operator[1] = '\0'; strcat(temporary_string, operator); strcat(temporary_string, operand_b); strcat(temporary_string, ")"); push_operand(temporary_string); } printf("\nInfix Expression:\t %s\n", operand_stack[top_operand]); return 0; } |
Output

In case you get any compilation errors or any doubts in this C Program For Conversion of Prefix Expression into Infix Expression, let us know about it in the Comment Section below.
im getting segmentation error for the above code of prefix to infix. can anyone please help me!!!!