logo CodeStepByStep logo

recursionMystery7

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

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?

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.