2

I wrote a recursion code, but I don't know why it won't work.(I changed some of the previous mistakes, but it still won't work :( )

The problem is:

Write a recursive method which

a. prints each character of the string reversed twice b. doubleReverse("hello") prints oolllleehh

The code I have so far is as below:

public class Recursion{
    public static void main(String[] args) {
        String s = "hello"; 
        doubleReverse(s); 
    }

    public static void doubleReverse(String s) {
        if (s == null || s.equals("")){
        return; 
    }

    System.out.print(s.charAt(s.length()-1) + s.charAt(s.length()-1) + doubleReverse(s.substring(1)));  
    }
}

The expected output doubleReverse("hello") prints oolllleehh

The output I'm getting is: won't compile

Error message:

2 errors found:

File: /Users/jaeahn/Desktop/CSCI /Practice/Recursion.java  [line: 12]
Error: /Users/jaeahn/Desktop/CSCI /Practice/Recursion.java:12: reference to print is ambiguous, both method print(char[]) in java.io.PrintStream and method print(java.lang.String) in java.io.PrintStream match

File: /Users/jaeahn/Desktop/CSCI /Practice/Recursion.java  [line: 12]
Error: /Users/jaeahn/Desktop/CSCI /Practice/Recursion.java:12: 'void' type not allowed here
Crazyjavahacking
  • 9,343
  • 2
  • 31
  • 40
CMSC
  • 157
  • 10

2 Answers2

4

You don't need to return anything, but you need to pass a smaller version of the string each time until you have printed all your characters. Here is one implementation ...

public static void recurse(String str){
    if(str.length() > 0) {
        System.out.print(str.charAt(str.length()-1));
        System.out.print(str.charAt(str.length()-1));
        recurse(str.substring(0, str.length() - 1));
    }
}
Constantin
  • 1,506
  • 10
  • 16
  • A recursive method must have a base case, otherwise your method will never end ... in our example, base case is an empty string – Constantin Jul 12 '15 at 01:58
  • Are you implicitly implying a base case in your code by setting str.length() to bigger than 0? – CMSC Jul 12 '15 at 02:38
  • the length of our string gets smaller and smaller because in the recursive call, we are chopping off the last character, and so the length is shorter by one each time. Eventually, the string is empty in which case our base case is reached and we stop recursion – Constantin Jul 12 '15 at 02:42
  • Yes, it is implied because otherwise our base case is not reached. Base case means STOP – Constantin Jul 12 '15 at 02:55
0

Your program has errors, because void method return an object "null":

public static void doubleReverse(String s) {    // <-- void method.
    if (s == null || s.equals("")) {
        return;                                 // <-- return object "null".
    }
}

In this case, you don't need recursion.

public class Solution {

    public static String reverseWords(String sentence) {
        String[] parts = sentence.split("");
        StringBuilder builder = new StringBuilder();
        builder.append(parts[parts.length - 1]).append(parts[parts.length - 1]);
        for (int i = parts.length - 2; i >= 0; --i) {
            builder.append("").append(parts[i]).append(parts[i]);
        }
        return builder.toString();
    }

    public static void main(String[] args) {
        System.out.println(reverseWords("hello"));
    }

}

//  Result:
//  oolllleehh


if you want use recurison

public class Solution {

    public static String reverse(String str) {
        String reverse = "";
        if (str.length() == 1) {
            return (str + str);
        } else {
            reverse += str.charAt(str.length() - 1);
            reverse += str.charAt(str.length() - 1) + reverse(str.substring(0, str.length() - 1));            
            return reverse;
        }
    }

    public static void main(String[] args) {
        System.out.println(reverse("hello"));
    }

}

//  Result:
//  oolllleehh
Vy Do
  • 46,709
  • 59
  • 215
  • 313