gpt4 book ai didi

c中for循环的条件

转载 作者:行者123 更新时间:2023-11-30 20:12:04 24 4
gpt4 key购买 nike

我有一个字符数组,我想创建一个循环,如果数组的下一个字符是数字,则循环跳转到下一个字符,如果不是,则打印最后一个数字。我可以/应该这样做吗?

int h;
char array[20];

for (h=0;isdigit(array[h]);h++)
h=h+1;
if(!isdigit(array[h]))
printf ("%d",h-1)

到目前为止,我在 for 循环中使用的唯一条件是“数字”条件,例如“h<=10”。

我正在使用 C 进行编程。

最佳答案

这是我对您的代码的分析:

int h;
char array[20];

for (h=0;isdigit(array[h]);h++)
h=h+1;
if(!isdigit(expression[h])) // "expression" is not declared.
printf ("%d",h-1) // Missing Semicolon
<小时/>

至于for循环中间可以放什么:
任何将评估为 True/False 的内容。
在 C 语言中,0 与 False 相同,任何其他数字都是 True。
isdigit 返回一个指示 true/false 的数字,因此它工作得很好。

<小时/>

这就是我认为你的意思:

int h;
char array[20];

for (h=0;isdigit(array[h]);h++) /**/; // While you have digits, go to the next character.

// Now that the loop is over:
printf ("%c",array[h-1]); // Print the previous character

关于c中for循环的条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37602392/

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