gpt4 book ai didi

c - 使用递归函数向后打印给定字符串,而不使用

转载 作者:行者123 更新时间:2023-11-30 21:05:14 24 4
gpt4 key购买 nike

我将总结我作业中的问题:

“编写一个递归函数,接收用户给出的字符数组,向后打印它并且不返回任何内容。该函数必须结束进程并在找到\0 时返回”。不要使用 string.h 库,也不要使用指针。”

我设法找到了递归方法,但我在函数定义方面失败了。

最佳答案

这只不过是对 Gardener 答案的修改,技术上没有使用指针(因为没有小*东西)。

#include <stdio.h>

void recursive_print(char array[]) {
if (array[0] != '\0') {
recursive_print(array + 1); //pointer arithmetic, but no '*', so not a pointer supposedly
printf("%c", array[0]);
}
else {
printf("\n"); // send a new line at the end to make it look better.
}
}

int main() {
char string[] = "Hello";
recursive_print(string);
return 0;
}

不过,我想补充一点,如果你的老师对指针的使用做出了这种(适得其反的)区分,你可能要记住,正如所有关于 C 的书籍并不都同样好一样,同样的事情也可以说导师数量。

关于c - 使用递归函数向后打印给定字符串,而不使用 <string.h>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55643662/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com