I'm studying C on K&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 < j)
reverse(s);
}
Main:
#include <stdio.h>
#include <string.h>
#define MAXLINE 100
void reverse(char s[]);
int main(void)
{
char s[MAXLINE] = "foo bar baz";
reverse(s);
printf("%s\n", s);
return 0;
}
0
toi
wheni == 0
. – Josay Feb 20 '12 at 14:44