logo CodeStepByStep logo

recursionMystery6

Language/Type: Java recursion

For each of the calls to the following recursive method below, indicate what value is returned:

public static String recursionMystery6(String s) {
    if (s.length() == 0) {
        return s;
    } else if (s.length() % 2 == 0) {
        String rest = s.substring(0, s.length() - 1);
        String last = s.substring(s.length() - 1);
        return last + recursionMystery6(rest);
    } else {
        String first = s.substring(0, 1);
        String rest = s.substring(1);
        return "(" + first + ")" + recursionMystery6(rest);
    }
}
recursionMystery6("hi");
recursionMystery6("quirk");
recursionMystery6("computer");

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.