Exercise 4.13. Write a recursive version of the function reverse(s), which reverses the
string s in place.
string s in place.
#include <stdio.h>
#include <string.h>
void reverse(char s[]);
int main()
{
char s[] = "Hello world";
reverse(s);
printf("%s\n", s);
return 0;
}
/* reverce: reverses the string in place */
void reverse(char s[])
{
char tmp;
static int i = 0;
int j = strlen(s) - (i+1);
if(i < j)
{
tmp = s[i];
s[i++] = s[j];
s[j] = tmp;
reverse(s);
}
}
Result:
Комментариев нет:
Отправить комментарий