因为main函数里调用reverse()函数的参数填错一个,up需要改为up-1, up为字符串结尾的空字符的下标,所以作为reverse()函数的参数,交换字符位置后,第一个字符为空字符,所以输出是空白的,只输出一个换行符,把reverse(s,low,up)改为reverse(s,low,up-1)即可,修改如下:
#include <stdio.h>
#include <string.h>
int main(void){
int low,up;
char s[1000];
void reverse(char s[],int l,int r);
gets(s);
up=strlen(s);
low=0;
reverse(s,low,up-1); // up为字符串的长度,s[up]即获取的字符串后面的空字符'\0',字符串里的最后一个字符的下标为up-1,所以这里填入up-1
printf("%s\n",s);
return 0;
}
void reverse(char s[],int low,int up){
char t;
if(low<up){
t=s[low],s[low]=s[up],s[up]=t;
low=low+1;
up=up-1;
reverse(s,low,up);
}
}