logo CodeStepByStep logo

recursionMystery6

Language/Type: C++ recursion
Author: Marty Stepp (on 2016/06/16)

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

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.

© Marty Stepp, all rights reserved.