gpt4 book ai didi

c - 这个 print_repeat 函数有什么问题?

转载 作者:太空宇宙 更新时间:2023-11-04 06:09:09 25 4
gpt4 key购买 nike

print_repeat 应该打印出字符串 s,但重复第 i 个字符 i 次(从 1 开始计数)。
所以 print_repeat("this") 应该打印 thhiiiissss

int print_repeat(char s[]){
int i,j;

i = 0;
while (s[i] != '\0');{
for (j = 1; j <= i+1; j+1){
putchar(s[i]);
}
}
return 0;
}

void main()
{
print_repeat("this");
}

最佳答案

在下面找到正确的代码:

    while (s[i] != '\0')
{
for (j = 1; j <= i+1; j++)
{
putchar(s[i]);
}
i++;
}

主要问题是:1. 在 while 循环中不增加变量 i2.变量j没有正确递增3.while后面有分号是错误的。

建议:尝试调试您的代码。这将帮助你学得更快。

关于c - 这个 print_repeat 函数有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4243352/

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