All mock tests are FREE till 31 December — register to save your progress

Consider the C/C++ function f() given below:

void f(char w[])
{
    int x = strlen(w); // length of the string
    char c;
    for (int i = 0; i < x; i++)
    {
        c = w[i];
        w[i] = w[x − i − 1];
        w[x − i − 1] = c;
    }
}

Which of the following is the purpose of f()?

Pick an option to check yourself, or open the answer below.

Show answer & explanation

Correct answer: B. It outputs the contents of the array in the original order.

Explanation

The loop runs over the whole string, so each pair is swapped twice (once at i and again at x − i − 1). The string ends up in its original order.

More Programming Languages and Computer Graphics questions