描述
我们把字符串 “cba” 称作字符串 “abc” 的反转串。
川草写了一个函数能够实现字符串 buf 的反转。其中 n 代表buf中待反转的字符串的长度。
你能猜猜川草写了什么?
请填写划线部分缺少的代码。
注意:本题请提交补全以后的所有代码(包括头文件)
[C]
#include <stdio.h>
#include <string.h>
#include <stdio.h>
#include <string.h>
void reverse_str(char* buf, int n)
{
if (n < 2)
return;
char temp = buf[0];
buf[0] = buf[n-1];
buf[n-1] = temp;
_____________________________; //填空
}
int main()
{
char str[100];
while (scanf("%s", str) != EOF)
{
reverse_str(str, strlen(str));
printf("%s\n", str);
}
return 0;
}
#include <stdio.h>
#include <string.h>
using namespace std;
void reverse_str(char* buf, int n)
{
if(n<2) return;
char tmp = buf[0];
buf[0] = buf[n-1];
buf[n-1] = tmp;
reverse_str(buf+1,n-2);
}
int main()
{
char str[100];
while (scanf("%s", str) != EOF)
{
reverse_str(str, strlen(str));
printf("%s\n", str);
}
return 0;
}