count_word函数在C中返回0

count_word函数在C中返回0,c,counter,C,Counter,这是我关于堆栈溢出的第一篇文章:) 我没有找到与我的问题相关的帖子,尽管有很多关于“数词”的帖子 我两周前开始学习C。我必须返回字符串中的字数,这是我目前正在进行的一个更大练习的一部分。我不明白为什么它不起作用,我想在这里得到一些建议 ft_strlen(char *str) //counting nb of char in the string { int size; size = 0; while (str[size]) size++;

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

我两周前开始学习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);
}
ft_strlen(char*str)//计算字符串中字符的nb
{
整数大小;
尺寸=0;
while(str[size])
大小++;
返回(大小);
}
整数英尺单词计数(字符*str)
{
int i;
整数大小;
整数计数;
i=0;
尺寸=英尺(str);
count_word=0;
当(str[i]
它返回0而不是5,奇怪的是,我不知道为什么。 如果我只使用strlen,它会像预期的那样返回“30”。所以ft_word_count有点问题

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

谢谢你的意见


查尔斯

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

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

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

问题在于这些线路

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++;
}
你会发现事情开始变得更有意义了。该代码将打印
4
。这仍然是错误的,但现在您有一些代码可以继续使用

一个简单的方法可以是:

while (i < size - 1)
{
    if (str[i] == ' ')
        count_word++;
    i++;
}
count_word++;
while(i
该代码将打印
5
。但是,代码太简单了,因为它将双空格计算为单词

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

错误部分

while (str[i] < size - 1)
while(str[i]
在这里,它在字符串的那个位置使用ascii值进行检查,这将始终为false,因此循环没有运行

正道

while (i < size - 1)
{
    if (str[i] == ' ')
        count_word++;
    i++;
}
count_word++;
while(i
正确计数时应忽略多个空格

i=0;
count_word=0;

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

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

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

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

  • 如果(i 126)
    不正确:检查分词符不是正确的方法,因为非ASCII字符将不被视为单词的一部分,而且编码可能不是ASCII。您应该改为使用
    中的
    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;
}
#包括
#包括
int ft_word_count(常量字符*str){
无符号字符c,last='';
整数计数=0;
对于(int i=0;(c=str[i])!='\0';i++){
如果(!isspace(c)和&isspace(last))
计数++;
last=c;
}
返回计数;
}
内部主(空){
char str[]=“我的妻子是一个大姑娘”;
printf(“%d\n”,ft_word_count(str));
返回0;
}

如果(I126)计数
i
是索引,而不是字符代码。1)
while(str[i]
-->
while(i2)
if(i126)
-->
if(str[i]'~')
?YEsss:)就是这样:)。它返回4而不是5,但我只需要在我的条件下工作。感谢您提供的帮助,如果
i
,您将获得
4
。使用
i“\n,空格和\t”,这正是练习要求我们的:),我只需将>=32放在这里,让它更简洁,谢谢你的提醒。如果校正器用几个空格和其他“低球”欺骗了我,我就处理了这个案子。^:如果((str[I]'~')&&&(str[I-1]>'&&str[I-1]<126))欢迎来到SO!也许值得添加一些注释来解释您正在做什么以及为什么要这样做,以便新来学习该语言的人可以从您的答案中学习。
i=0;
count_word=0;

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

     if(!toggle && str[i]!= ' ')
        count_word++;
     toggle=1;
  }
  else
    toggle=0;
  i++;
}
#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;
}