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

实现:将参数字符串中的字符反向排列。
要求:不能使用C函数库中
的字符串操作函数。

int my_strlen(const char *str) //自定义的计算字符串长度的函数
{

	int count = 0;
	while (*str)
	{
		count++;
		str++;
	}
	return count;
}

void reverse_string(char *str)
{

	char temp = 0;
	int len = my_strlen(str);
	if (len > 0)
	{
		temp = str[0];
		str[0] = str[len - 1];
		str[len - 1] = '\0';

		reverse_string(str + 1);
		str[len - 1] = temp;
	}
}
int main()
{
	char arr[] = "abcdef";
	reverse_string(arr);
	printf("%s\n", arr);
	system("pause");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_41892460/article/details/82820837