【问题标题】:How to split up a string and count how many times a word is used?如何拆分字符串并计算一个单词的使用次数?
【发布时间】:2016-03-13 19:27:02
【问题描述】:
    while(token != NULL)
    {
    //      for(position = strcspn(str,token); position >= 0;
    //              position = strcspn(str, token + 1));
    //      {
    //              str2[position] = count++;
    //      }



    }

我认为我的代码存在逻辑问题。我试图从用户输入中获取一个字符串并返回每个单词被使用的次数,并且每个单词只返回一次。我认为我的问题在我已注释掉的部分中,但我不完全确定如何修复或更改我的代码。 例如: 输入:你好,我的猫在说你好。 输出:你好 2 我的 1 猫 1 是 1 说 1

【问题讨论】:

  • 我确信str[size] = read_line(str, size); 不可能是正确的。您正在将行输入函数的返回值分配给单个 char。 OTOH,如果您尝试将返回的指针分配给 str,则不能,因为它是一个固定数组。
  • str[size] = ... 不正确的另一个原因是您在数组边界之外进行索引。这里最大的合法数组索引是str[size-1]。您是否启用了警告?请张贴Minimal, Complete, and Verifiable example,显示您已尝试过的内容。
  • 不,直到您显示所有相关信息,然后才显示字符串输入。 read_line() 是什么?有很多关于计算字符串中单词的问题。
  • 我已经要求你两次发布read_line 功能,所以我现在放弃了。对于之前的问题,点击问题下方的c标签,然后在右上角附近的SO搜索框中输入词频
  • 我认为您需要为数组的每个元素维护一个计数。您已计数为标量整数。然后让 maxCount 说出哪个是最大的 (of the count) 。两者都可以在同一个循环中更新。

标签: c string strtok


【解决方案1】:

我已经修改了你的代码并且写的有点不同, 请看一下。

int main()
{
   char haystack[50] = "Hello my cat is saying Hello";
   char needle[10];
   int i = 0,j = 0,k = 0;
   char *ret = NULL;
   int cnt = 0;


   while(haystack[i] != NULL)
   {
       if(haystack[i] == ' ')
       {
           i++;
       }
       else
       {
           //Get each sub strings.
           while((haystack[i] != ' ') && (haystack[i] != NULL))
           {
               needle[k++] = haystack[i];
               i++;
           }
           needle[k] = '\0';
           printf("The substring is: %s", needle);

           //Find how many times the sub string is there in the string 
           while(strstr(haystack, needle) != NULL)
           {
               ret = strstr(haystack, needle);
               //Once the Substring is found replace all charecter of that substring with space.
               for(j=0;j<k;j++)
               {
                   *(ret+j) = ' ';
               }
               cnt++;//Count the no of times the substrings found.
           }
           printf("= %d\n",cnt);
           cnt = 0;
           k = 0;
       }
   }

   return(0);
}

特殊字符我没有处理,你可以修改处理。

所以我使用了字符串"Hello my cat is saying Hello" 而不是"Hello, my cat is saying Hello."。删除了逗号。

希望这会有所帮助。

【讨论】:

    【解决方案2】:

    要计算一个单词在字符串或行中出现的次数,您需要一个结构来保存所有不同的单词,主要是每个单词出现的频率。

    我的简单方法,没有任何优化,是:

    #include <stdio.h>
    #include <stdlib.h>
    
    struct wordsDetail
    {
        char word[100];
        int freq;
    } wordsDetail;
    
    void updateWords(struct wordsDetail s[],  int length, char *token)
    {
        int i;
    
        for (i = 0; i < length && s[i].word[0] != '\0'; i++) {
            if (strcmp(s[i].word, token) == 0) {
                s[i].freq++;
                return;
            }
        }
        strcpy(s[i].word, token);
        s[i].freq++;
    }
    
    void printResults(struct wordsDetail s[], int length) {
        printf("Words\tFreq\n");
        for (int i = 0; i <length && s[i].word[0] != NULL; i++) {
            printf("%s\t%d\n", s[i].word, s[i].freq);
        }
    }
    
    int main(void)
    {
        struct wordsDetail myWords[100];
        int wordsDetailLength = sizeof(myWords) / sizeof(wordsDetail);
    
        const size_t line_size = 1024;
        char *str = NULL;
        int *str2 = NULL;
        int i = 0;
        char *token;
    
        for (i = 0; i < wordsDetailLength; i++) {
            myWords[i].word[0] = '\0';
            myWords[i].freq = 0;
        }
    
        if ((str = calloc(line_size, sizeof(char))) == NULL) {
            printf("error\n");
            exit(-1);
        }
        printf("Input: ");
    
        if (scanf("%[^\n]", str) != 1) {
            printf("error\n");
            exit(-1);
        }
    
        printf("Output: \n");
    
        token = strtok(str, " .,!");
    
        while (token != NULL) {
            updateWords(myWords, wordsDetailLength, token);
            token = strtok(NULL, " .,!");
        }
        printResults(myWords, wordsDetailLength);
        return 0;
    }
    

    一个简单的结果是:

    Input: Hello, my cat is saying Hello to my cat.
    Output:
    Words   Freq
    Hello   2
    my      2
    cat     2
    is      1
    saying  1
    to      1
    

    【讨论】:

      猜你喜欢
      • 2018-06-06
      • 2019-08-23
      • 2012-12-03
      • 2014-04-29
      • 2011-04-30
      • 1970-01-01
      • 2016-06-28
      相关资源
      最近更新 更多