编写一个函数reverse_string(char * string)(递归实现)

实现:将参数字符串中的字符反向排列。

要求:不能使用C函数库中的字符串操作函数。

#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
int my_strlen(const char*str)
{
assert(str);
int count = 0;
while (*str)
{
count++;
str++;
}
return count;
}
void reverse_string(char*str)
{
assert(str);
char tmp = 0;
int len = my_strlen(str);
if (len > 0)
{
tmp = str[0];
str[0] = str[len - 1];
str[len - 1] = '\0';
reverse_string(str + 1);
str[len - 1] = tmp;
}
}
int main()
{
char arr[] = "abcdef";
reverse_string(arr);
printf("%s\n", arr);
system("pause");
return 0;
}


结果:

 

fedcba

请按任意键继续. . .