Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3353452
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:09:11+00:00 2026-05-18T02:09:11+00:00

I’m trying to implement a program that changes a prefix expression to a postfix

  • 0

I’m trying to implement a program that changes a prefix expression to a postfix one using recursion.

I’ve written what I thought would work but instead of the output ab/c*de+f*- I get aa/aa/*aa/aa/*- instead.

I think my code is getting stuck when I try to get the first character of String pre or when I try to delete the first character of String pre. Any suggestions/comments?

  public class Prefix2Postfix {
        public static final String prefixInput ="-*/abc*+def";
        //desired postfix output is "ab/c*de+f*-"

        public static void main (String[] args){
            System.out.println(pre2Post(prefixInput));
        }

        public static String pre2Post(String pre){
            //find length of string
            int length = pre.length();

            //ch = first character of pre
            char ch = pre.charAt(0);

            //delete first character of pre
            pre = pre.substring(1,length);
            if(Character.isLetter(ch)){
                //base case: single identifier expression
                return (new Character(ch)).toString(ch);
            }else{ 
                //ch is an operator
                String postfix1 = pre2Post(pre);
                String postfix2 = pre2Post(pre);
                return postfix1 + postfix2 + ch;
            }
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T02:09:12+00:00Added an answer on May 18, 2026 at 2:09 am

    So the error in your code has to do with where you calculate postfix1 and postfix2 — note that you’re not offsetting postfix2.

    To do this recursion you need to understand a few cases:

    • When you encounter an operator you need to recurse and move the operator to the right, and then process any remaining portion of the string that has not been processed
    • When you encounter a letter and an operator you should just return the letter
    • When you encounter two letters, you should just return those two letters

    This means when you encounter something like +-abc you will do the following steps:

    f("+-abc") => return f("-abc") + "+" + f(rem1)
     f("-abc") => return f("abc") + "-" + f(rem2)
      f("abc") => return "ab"
      rem2 = "c" (remainder of the string)
      f("c")   => return "c"
     rem1 = ""   (nothing left in the string to parse)
    
    which constructs "ab-c+"
    

    This should work:

    public static String pre2post(String pre){
        if(pre.length() <= 1){
            return pre;
        }
    
        if(!Character.isLetter(pre.charAt(0))){
            String a = pre2post(pre.substring(1)) + pre.charAt(0);
            String b = pre2post(pre.substring(a.length()));
            return a + b;
        }else if(!Character.isLetter(pre.charAt(1))){
            return pre.substring(0,1);
        }else{
            return pre.substring(0,2);
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.