logo CodeStepByStep logo

recursion_mystery15

Language/Type: PHP recursion recursion mystery

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

function recursion_mystery15($s) {
    if (strlen($s) == 0) {
        return $s;
    } elseif (strlen($s) % 2 == 0) {
        $rest = substr($s, 0, strlen($s) - 1);
        $last = $s[strlen($s) - 1];
        return $last . recursion_mystery15($rest);
    } else {
        $first = $s[0];
        $rest = substr($s, 1);
        return "($first)" . recursion_mystery15($rest);
    }
}
recursion_mystery15("hi");
recursion_mystery15("php");
recursion_mystery15("quirk");
recursion_mystery15("recursion");

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.