Recursive string reverse function I'm studying C on K&amp;R and now I'm facing a recursion exercise that states:


  Write a recursive version of the function reverse(s), which reverses the string s in place.


I've written the code below and I'm pretty sure that it works.  I'll be glad to receive some critiques about it.

Reverse function:

/* reverse: reverse string s in place */
void reverse(char s[])
{
    static int i, j = 0;
    int c;

    if (i == 0) {
        i = 0;
        j = strlen(s)-1;
    }
    c = s[i];
    s[i] = s[j];
    s[j] = c;
    i++;
    j--;
    while(i &lt; j)
        reverse(s);
}


Main:

#include &lt;stdio.h&gt;
#include &lt;string.h&gt;

#define MAXLINE 100

void reverse(char s[]);

int main(void)
{
    char s[MAXLINE] = "foo bar baz";

    reverse(s);
    printf("%s\n", s);
    return 0;
}
While this is recursive, it entirely misses the point.  As the while loop will only ever execute once (because the function it calls only returns when i &lt; j it could just as well have been an if, and thus all you've got is tail-recursion.

What you have is equivalent to the following, but impossible to call twice and maybe less efficient:

void reverse(char s[])
{
    int i = 0, j = strlen(s)-1;
    int c;
    while (i &lt; j) {
        c = s[i];
        s[i] = s[j];
        s[j] = c;
        i++;
        j--;
    }
}


That's obviously not recursive.  The way you'd do it recursively is always swap the outer two characters of the string and then pass pointers to the beginning and end of a substring along.

Apart from that, you're initialising j but not i, which makes little sense: both will be 0 initially anyway, so at least be consistent.  You're also not using MAXLINE, so you might as well get rid of it (if C allows it).  You're also not checking for a NULL being passed in.