C //练习 4-13 编写一个递归版本的reverse(s)函数,以将字符串s倒置。

发布时间:2024年01月10日

C程序设计语言 (第二版) 练习 4-13

练习 4-13 编写一个递归版本的reverse(s)函数,以将字符串s倒置。

注意:代码在win32控制台运行,在不同的IDE环境下,有部分可能需要变更。
IDE工具:Visual Studio 2010

?

代码块:
#include <stdio.h>
#include <stdlib.h>

static int i = 0;
static int j = 0;

void reverse(char s[]){
	char c = '\0';

	if(s[i] != '\0'){
		c = s[i++];
		reverse(s);
	}
	if(j < i && c != '\0'){
		s[j++] = c;
	}
}

int main(){
	char s[] = "hello world!";
	reverse(s);
	printf("%s\n", s);

	system("pause");
	return 0;
}
文章来源:https://blog.csdn.net/navicheung/article/details/135511983
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。