gpt4 book ai didi

c - for循环不运行

转载 作者:太空狗 更新时间:2023-10-29 16:02:22 25 4
gpt4 key购买 nike

我想出了我认为是一个棘手(尽管是标准)问题的困惑解决方案:对于给定的用户输入,反转单词的字母

例如:

This is a standard test

成为

sihT si a dradnats tset

不是

tset dradnats a si sihT

事情的核心是这段代码

  while (!iscntrl(user_input[x])) // quit when new line is read
{
restart:
x++;
puts("first level test");
if (user_input[x]==' ')
{
puts("second level test");
for (i=x; user_input[i]!=' '; --i)
{
reverse_words[k]=user_input[i];
k++;
puts("third level test");
goto restart;
}
}
}

(是的,我知道,那里有一个 goto :/)

但循环的第三层从未被触及。

大概是 (i=x; user_input[i]!=' '; --i) 作为 for 循环参数完全错误?

xik 在第一个循环开始之前都被初始化为整数 == 0。

最佳答案

好吧,你的条件在这里自相矛盾:

 if (user_input[x]==' ')
{
puts("second level test");
for (i=x; user_input[i]!=' '; --i)

只有 user_input[x] 是空格时才输入,但只要它不等于空格就会循环。

关于c - for循环不运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13961575/

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