如何拆分字符串并计算一个单词的使用次数?

时间:2016-03-13 19:27:03

标签: c string strtok

    while(token != NULL)
    {
    //      for(position = strcspn(str,token); position >= 0;
    //              position = strcspn(str, token + 1));
    //      {
    //              str2[position] = count++;
    //      }



    }

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

2 个答案:

答案 0 :(得分:0)

我修改了你的代码并且编写的内容略有不同, 请看看。

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."。删除了逗号。

希望这有助于。

答案 1 :(得分:0)

要计算单词在字符串或行中出现的次数,您需要一个结构来保留您拥有的所有不同单词,以及每个单词频繁出现的次数。

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

#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
相关问题