反转串
我们把“cba”称为“abc”的反转串。
下面的代码可以把buf中的字符反转。其中n表示buf中待反转的串的长度。请补充缺少的代码。
#include <stdio.h>
#include <string.h>
void reverse_str(char *buf,int n)
{
char tmp;
if(n < 2) return ;
tmp = buf[0];
buf[0] = buf[n - 1];
buf[n - 1] = tmp;
reverse_str(buf+1,n - 2);
}
int main()
{
char ch[] = "abcdefg";
reverse_str(ch,strlen(ch));
printf("%s\n",ch);
return 0;
}