logo CodeStepByStep logo

recursionMystery7

Language/Type: C++ recursion

For each call to the following recursive function, write the output that is produced as it would appear on the console. Recall that relational operators like < and > compare strings by alphabetical order; for example, "a" is less than "b".

void recursionMystery7(string s) {
    if (s.length() <= 1) {
        cout << s;
    } else {
        string first = s.substr(0, 1);
        string last  = s.substr(s.length() - 1, 1);
        string mid   = s.substr(1, s.length() - 2);
        if (first < last) {
            recursionMystery7(mid);
            cout << last << toUpperCase(first);
        } else {
            cout << "[" << first << "]";
            recursionMystery7(mid);
            cout << 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.