3

For some reason when the string length is zero, it does not come out of the while loop. Can some one help me on this?

static String str1 = "";

public static void reverse(String str) {
    while (str.length() > 0) {
        str1 = str1 + str.charAt(str.length() - 1);
        StringBuffer str_buf = new StringBuffer(str);
        str = str_buf.deleteCharAt(str.length() - 1).toString();
        reverse(str);
    }
    System.out.println("String is " + str1);
}
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
prc
  • 43
  • 2

3 Answers3

4

Replace while with if

if(str.length()>0)

Update:

The reason while fails is, after str.length() becomes 0, it hits the bottom of recursion, and control returns to the "higher" level, where str.length() is still 1. So it again calls itself.

So with while ,after it reaches 0, it will keep looping continuously between 1 and 0.

sanjeev mk
  • 4,276
  • 6
  • 44
  • 69
  • but why did the while loop not work? After all it was also checking the condition. – prc Dec 24 '13 at 11:34
  • Besides, Strings are passed by value, not by reference. So when you do str = str_buf.deleteCharAt(str.length() - 1).toString(); within an inner recursion it does not change the value of str in the method that called the current method, i.e. the previous recursion – Enrique Fueyo Dec 24 '13 at 11:48
  • @EnriqueFueyo Yeah, but that doesn't matter. The trick to this working is the fact that `str1` is outside of the method, so it transcends which method call you're looking at. – Dawood ibn Kareem Dec 24 '13 at 11:50
  • @DavidWallace it does matter since the while-loop is checking whether str.length is > than 0 or not, but str never changes – Enrique Fueyo Dec 24 '13 at 11:52
  • But `str` does change. Every time you write `str = str_buf.deleteCharAt(str.length() - 1).toString();` it changes. The only problem is that there are multiple copies of `str`. But all of them get shorter, and get to zero length eventually. This program does terminate, even with the `while` present. It just prints a lot more stuff than you might expect it to. – Dawood ibn Kareem Dec 24 '13 at 12:00
  • @DavidWallace, yes! you're completely right. I was focused on the pass-by ref/value issue so I forgot about that. Thanks! – Enrique Fueyo Dec 24 '13 at 12:04
2

The way that you have it coded you are looping through the entire string and recursively calling the function. So for a short string "abcd" the first time through will call reverse with "abc", "ab", and "a". The reverse call to "abc" will call reverse with "ab" and "a". If you are recursively calling the function then you don't need the while loop as @sanjeev-mk suggested instead you just need the if as the exit condition.

ryknow
  • 161
  • 1
  • 10
0

I believe there is a more suitable code to recursively print string reverse:

public void reverseStringPrint(String inputString, int index){
    if (index < (inputString.lenght() - 1))
        reverseStringPrint(inputString, index + 1);
    System.out.print(inputString.charAt(index);
}

Run with index = 0.

In order to get the reversed string at the output (not only print it) you may do it like this:

public String reverseString(String inputString, int index){
       String restOfTheString = "";
       if (index < (inputString.lenght() - 1))
           restOfTheString = reverseStringPrint(inputString, index + 1);
       return charAt(index) + restOfTheString;
   }
3yakuya
  • 2,622
  • 4
  • 25
  • 40