logo CodeStepByStep logo

recursionMystery4X

Language/Type: C++ recursion

For each of the calls to the following recursive function below, indicate the final state of the string that was passed, as well as the value that was returned by the function:

int recursionMystery4X(string& s, int i, int j, char k) {
    if (i >= j) {
        return i;
    } else if (s[i] < k) {
        return recursionMystery4X(s, i + 1, j, k);
    } else if (s[j] > k) {
        return recursionMystery4X(s, i, j - 1, k);
    } else {
        int temp = s[i];
        s[i] = s[j];
        s[j] = temp;
        return recursionMystery4X(s, i + 1, j - 1, k);
    }
}
// a)
//          0123456
string s = "OXIDIZE";
mysteryX(s, 0, 6, 'K')

// b)
//          01234567890123456
string s = "TCTTCGTCCGAACCAGA";
mysteryX(s, 0, 16, 'F')
s after call a)
value returned from call a)
s after call b)
value returned from call b)

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.