Page 88
Write a recursive version of the function reverse(s)
, which reverses the string s
in place.
#include <stdio.h>
#include <string.h>
void reverse(char str[], int begin, int end);
int main()
{
char str[] = "Reverse me";
reverse(str, 0, strlen(str) - 1);
printf("%s", str);
}
void reverse(char str[], int begin, int end)
{
static char temp;
temp = str[begin];
str[begin] = str[end];
str[end] = temp;
if (end - begin > 2) {
reverse(str, ++begin, --end);
}
}