【问题标题】:While (isspace(array))而(isspace(数组))
【发布时间】:2016-09-28 14:43:59
【问题描述】:

当i位置为空白时,如何让“while”函数停止?

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>

int main ()
{

    int i,t;
    char array[20];

    t=strlen(array);

    for (i=0;i<=t;i++)
    {
        if(isspace(expressio[i])){
            while (expressio[i+1]!=isspace(expressio[i+1]))
                i=i+1;
            if (isspace(expressio[i+1]))
                { code follows here...}

我可以这样吗?

我正在用 C 编程。

【问题讨论】:

  • 你说的stop是什么意思?如果你想打破循环,使用break;
  • while (!isspace(expressio[i+1])) ?
  • 请注意,break 只会让您脱离 current 循环。所以如果你在上面的while 循环中调用break,你仍然会在for 循环中。如果你想跳出所有循环,你可以 1) 使用goto 跳转到所有循环之外的标签 2) make i&gt;t,这样for 循环的条件现在失败,或者3) return 如果您在那时完成了该功能。
  • 我的意思是,我不知道怎么对程序说:当你在数组中找到一个位置是空格时,告诉我这两个空格之间有多少个位置(例如)

标签: c arrays ctype


【解决方案1】:

不,你不能这样做。

while (expressio[i+1]!=isspace(expressio[i+1]))

没有意义。 expressio[i+1] 是一个字符,而isspace(expressio[i+1]) 是 1 或 0。您将苹果与橙子进行比较。

另外,

char array[20];

t=strlen(array);

也没有任何意义。如果您将字符串写入array,则strlen 将返回该字符串的长度。在初始化 array 之前调用 strlen 是没有用的(它可能只返回任何东西)。

【讨论】:

    猜你喜欢
    • 2012-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多