logo CodeStepByStep logo

recursionMystery7

Language/Type: Java recursion

For each call to the following recursive method, write the output that is produced as it would appear on the console.

public static void recursionMystery7(String s) {
    if (s.length() <= 1) {
        System.out.print(s);
    } else {
        String first = s.substring(0, 1);
        String last  = s.substring(s.length() - 1, s.length());
        String mid   = s.substring(1, s.length() - 1);
        if (first.compareTo(last) < 0) {
            recursionMystery7(mid);
            System.out.print(last + first.toUpperCase());
        } else {
            System.out.print("[" + first + "]");
            recursionMystery7(mid);
            System.out.print(last);
        }
    }
}
recursionMystery7("abcd");
recursionMystery7("leonard");
recursionMystery7("breakfast");

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.