0
public static void main(String[] args)
{
    String string="HelloWorld";
    char ch = string.charAt(string.length()-1);
    maximum(string,ch,string.length()-2);
}

public static void maximum(String string,char ch,int length)
{
    if(length==0)
        return;
    System.out.println(ch);
    maximum(string,string.charAt(length),length-1);
}

It prints

d
l
r
o
w
o
l
l

instead of

d
l
r
o
w
o
l
l
e
h

Why?

1
  • Maximum isn't a very good name because it doesn't indicate its actual purpose. The maximum of a string would probably be the 'highest' letter in the string, probably the letter furthest in the alphabet. Commented Apr 13, 2017 at 19:27

2 Answers 2

4

Well doing recursion here isn't something you should actually do. However, assuming this is a learning exercise, I see two problems here:

  1. you are returning before printing the index 0 character.
  2. you are retrieving the current index only during the call to the next recursive level. Meaning your ch is always one behind index.

maybe do something like:

public static void main(String[] args)
{
    String string="HelloWorld";
    maximum(string, string.length()-1);
}

public static void maximum(String string, int length)
{
    System.out.println(string.charAt(length))
    if(length==0)
        return;
    maximum(string, length-1);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Could even overload maximum to not take a length parameter and just call the maximum with the string's length public static void maximum(String string) { maximum(string.length() - 1); }
2

This is a logic problem, not a code problem. First of all, I suggest printing out the value of length at each recursion. That will hopefully reveal the problem. You could even do this by hand and see the problem.

Overall, you are treating the variable length inconsistently. Before the recursion, you correctly subtract 1 to get the zero-based-index position of the last character. But then you decrease length by 2 as you start the recursion, even though you have retrieved only 1 character from the end. Right there you changed the meaning of length to mean the last position instead of the length of the remaining string. But inside maximum() you treat it like the string length in one part of the code and like the last position in another part.

Either way, you need to change the code inside maximum() to treat the variable consistently. Depending on what meaning you choose, you may need to change the argument values that you initially send to maximum().

1 Comment

Since I started writing my answer, others have responded with good coding advise. I agree that this problem doesn't require recursion and is not setup efficiently at that. But this is such a simple problem, rewriting the code will not help understand what the logic problem was in the first place, hence my emphasis on the contradictions in the variable name and how you treated it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.