c - for循环不运行

标签 c for-loop nested-loops

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

例如:

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/

相关文章:

c - 这行简单的代码中的变量定义在哪里?

c - 使用第二个参数的 ReadConsoleOutputCharacter 错误

c - 如果WriteFile同步完成,是否发出信号通知事件

html - 使用 for 循环或 lapply 将 Web 抓取到具有相似 URL 的 R 多个链接

amazon-web-services - 如何在 terraform 资源中的 zipmap 和列表上创建嵌套 for 循环

c - 无法在 Mac OSX 上的程序集 (x86-64) 和 C 文件中链接 printf 和 scanf

java - Java 中的数组列表

iphone - 索引对象矩阵 (UILabel)

c++ - 查找未排序数组的模式,以及该数组是否具有多个模式或没有模式

javascript - 使用 for 循环收集电影评级