logo CodeStepByStep logo

recursionMystery6

Language/Type: C++ recursion

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

string recursionMystery6(string s) {
    if (s.length() == 0) {
        return s;
    } else if (s.length() % 2 == 0) {
        string rest = s.substr(0, s.length() - 1);
        string last = s.substr(s.length() - 1, 1);
        return last + recursionMystery6(rest);
    } else {
        string first = s.substr(0, 1);
        string rest = s.substr(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.