【问题标题】:count_word function returns 0 in Ccount_word 函数在 C 中返回 0
【发布时间】:2017-09-15 03:34:33
【问题描述】:

这是我关于堆栈溢出的第一篇文章 :) 尽管有很多关于“计数单词”的帖子,但我没有找到与我的问题相关的帖子。

我在 2 周前开始使用 C。我必须返回一个字符串中的单词数,这是我目前正在做的一个更大的练习的一部分。我不知道为什么它不起作用,我在此请求一些提示。

ft_strlen(char *str) //counting nb of char in the string
{
    int     size;

    size = 0;
    while (str[size])
        size++;
    return (size);
}

int     ft_word_count(char *str)
{
    int     i;
    int     size;
    int count_word;

    i = 0;
    size = ft_strlen(str);
    count_word = 0;
    while (str[i] < size - 1) //counting nb of words in the string, I added "-1" to size to get rid of the '\0'
    {
        if (i <= 32 || i > 126 ) //defining what will make a word
            count_word++;
        i++;
    }
    return (count_word);
}

int     main(void)
{
    char    str[]="Meine Frau liebt grosse Pferde";

    ft_strlen(str);
    printf("%d", ft_word_count(str));
    return (0);
}

它返回 0 而不是 5,奇怪的是,不知道为什么。 如果我只使用我的 strlen,它会按预期返回“30”。所以 ft_word_count 有问题

使用 gcc 编译。 语法并不简洁,但这是我学校要求的规范的一部分。

感谢您的意见

查尔斯

【问题讨论】:

  • if (i &lt;= 32 || i &gt; 126 ) count_word++; : i 是索引,而不是字符代码。
  • 1) while (str[i] &lt; size - 1) --> while (i &lt;= size)
  • 2) if (i &lt;= 32 || i &gt; 126 ) --> if (str[i] &lt;= ' ' || (unsigned char)str[i] &gt; '~' ) ?
  • 是的 :) 就是这样 :)。它返回 4 而不是 5 但我只需要根据我的情况工作。感谢您的帮助
  • 如果i &lt; size-1,你会得到4。使用i &lt;= size 代替它。

标签: c counter


【解决方案1】:

你应该忽略多个空格来计算正确

i=0;
count_word=0;

while(str[i]>0)
{
  if((str[i]!= ' '))
  {

     if(!toggle && str[i]!= ' ')
        count_word++;
     toggle=1;
  }
  else
    toggle=0;
  i++;
}

【讨论】:

  • 欢迎来到 SO!可能值得添加一些 cmets 来解释您在做什么以及为什么这样做,以便该语言的新手可以从您的答案中学习。
【解决方案2】:

我相信你的意思是更像这样使用逻辑:

 if(str[i] <= 32 || str[i] > 126) count_word++;

在您发布的代码中,您查看的是索引的值,而不是字符串中的字符值。

即便如此,这并不是您收到“0”的原因。造成这种情况的原因是您的while 条件。您正在检查字符串中的数字 ASCII 值是否大于字符串的长度……我可以向您保证,确实如此。因此,您还想将白色更改为:

 while(i < size - 1)

就个人而言,我可能会检查 \n、空格和 \t,但每个人都有自己的!

【讨论】:

  • "\n, space and \t",这正是练习要求我们的:),我只是把 >= 32 让它简洁
【解决方案3】:

问题出在这几行

while (str[i] < size - 1)      // Here you compare the individual chars and
                               // the length of the string. That makes
                               // no sense
{
    if (i <= 32 || i > 126 )   // Here you compare the index i and some
                               // fixed numbers. That makes no sense
        count_word++;
    i++;
}

您似乎已经交换了两者,即您在应该使用i时使用str[i],而在应该使用str[i]时使用i

因此,如果您将代码更改为:

while (i < size - 1)
{
    if (str[i] <= 32 || str[i] > 126 )
        count_word++;
    i++;
}

您会发现事情开始变得更有意义。该代码将打印4。这仍然是错误的,但现在您有了一些可以继续使用的代码。

一个简单的方法可能是:

while (i < size - 1)
{
    if (str[i] == ' ')
        count_word++;
    i++;
}
count_word++;

该代码将打印5。但是,代码过于简单,因为它将双空格算作单词。

换句话说 - 您需要添加更多代码来处理这种情况,但我想这是学习过程的一部分。祝你好运。

【讨论】:

  • 是的,感谢您提及。如果校正器用几个空格和其他“低球”欺骗我,我就成功了 ^^: if ((str[i] '~') && (str [i - 1] > ' ' && str[i - 1]
【解决方案4】:

错误部分

while (str[i] < size - 1)

这里它检查字符串那个地方的 ascii 值,这个值总是假的,因此循环没有运行。

正确的方法

while (i < size - 1)
{
    if (str[i] == ' ')
        count_word++;
    i++;
}
count_word++;

【讨论】:

    【解决方案5】:

    您的代码中存在多个问题:

    • while (str[i] &lt; size - 1) 不正确,因为您将字符的值与字符串的大小而不是索引进行比较:它应该是 while (i &lt; size)

    • if (i &lt;= 32 || i &gt; 126 ) 不正确:这不是检查单词分隔符的正确方法,因为非 ASCII 字符不会被视为单词的一部分,并且编码可能无论如何都不是 ASCII。您应该改用&lt;ctype.h&gt; 中的isspace()

    • 此外,计算空格并不是计算单词的方法。您应该计算从空间到非空间的转换次数。

    这是一个更简单的版本:

    #include <ctype.h>
    #include <stdio.h>
    
    int ft_word_count(const char *str) {
        unsigned char c, last = ' ';
        int count = 0;
    
        for (int i = 0; (c = str[i]) != '\0'; i++) {
             if (!isspace(c) && isspace(last))
                 count++;
             last = c;
        }
        return count;
    }
    
    int  main(void) {
        char str[] = "Meine Frau liebt grosse Pferde";
    
        printf("%d\n", ft_word_count(str));
        return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 2018-07-15
      • 2015-03-09
      • 2013-09-02
      • 1970-01-01
      • 2019-12-26
      • 2015-12-03
      • 2012-03-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多