Recursive function that reverse the words in a string
The program below is just for fun.

Since the program asks for recursion, then in principle, recursion should be the sole iteration technique. Assuming that the requirement is to only implement a single function, and that string is '\0' terminated, the following program illustrates how this is possible.

Basically, the technique is to use the value of count control the mode of the reverse function. There are three modes: initial call, reverse each word, and reverse string.

A positive count signals the initial call, and count represents the length of the string. On initial call, the program follows the algorithm recipe of: first reverse the input string, then reverse each word in the input string.

A count value of zero causes the function to find the beginning and end of the first word in the string, reverse it, and recursively repeat the process beginning after the just reversed word.

A negative count causes the function to reverse the string assuming its length is -(count + 1). The off by one length is used to make sure the string reversal case does not run into the word reversal case. Thus, a -1 count represents an empty string.

void reverse (char *string, int count) {
    if (string == NULL || *string == '\0') return;
    // positive count := initial call
    if (count &gt; 0) {
        reverse(string, -count-1); // reverse the whole string
        reverse(string, 0);        // reverse each word in whole string
        return;
    }
    // zero count := reverse each word in the string
    // skip over whitespace, then find end of word, reverse word, repeat
    if (count == 0) {
        char *p = string;
        int sl = strspn(p, " \t");
        int wl = strcspn(p + sl, " \t");
        reverse(p + sl, -wl-1);    // reverse the found word
        reverse(p + sl + wl, 0);   // reverse each word in rest of string
        return;
    }
    // negative count := reverse string of length -(count + 1)
    // reverse the string by swapping first and last, then shrink
    count = -(count + 1);          // fix-up count value
    if (count &lt; 2) return;
    char t = string[0];
    string[0] = string[count-1];
    string[count-1] = t;
    reverse(++string, ---count);
}


Note the lack of looping structures within the function. All loops are accomplished with recursion or hidden by standard library functions strspn and strcspn.