计算每个单词在给定句子中出现的次数

时间:2018-12-15 09:06:13

标签: c word-count

我想计算给定句子中出现了多少个单词。我正在使用C编程语言。它不能算出最后一个字。在给定的字符串中,它计算每个单词出现的次数。如果有类似red green blue blue green blue的句子,则程序应计算red 2 green 2 and blue 3。但就我而言,它不算作blue 3。而不是先计算blue 2,然后计算blue 1

red 1 
green 2 
blue 2 
blue 
1

我的代码:

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

int main(void)
{
    int count = 0, c = 0, i, j = 0, k, space = 0;
    char str[1000], p[500][1000], str1[200], ptr1[500][1000];
    char *ptr;
    fgets(str, sizeof(str), stdin);
    for (i = 0;i<strlen(str);i++)
    {
        if ((str[i] == ' ')||(str[i] == ', ')||(str[i] == '.'))
        {
            space++;
        }
    }
    for (i = 0, j = 0, k = 0;j < strlen(str);j++)
    {
        if ((str[j] == ' ')||(str[j] == 44)||(str[j] == 46))  
        {    
            p[i][k] = '\0';
            i++;
            k = 0;
        }        
        else
             p[i][k++] = str[j];
    }
    k = 0;
    for (i = 0;i <= space;i++)
    {
        for (j = 0;j <= space;j++)
        {
            if (i == j)
            {
                strcpy(ptr1[k], p[i]);
                k++;
                count++;
                break;
            }
            else
            {
                if (strcmp(ptr1[j], p[i]) != 0)
                    continue;
                else
                    break;
            }
        }
    }
    for (i = 0;i < count;i++) 
    {
        for (j = 0;j <= space;j++)
        {
            if (strcmp(ptr1[i], p[j]) == 0)
                c++;
        }
        printf("%s %d\n", ptr1[i], c);
        c = 0;
    }
    return 0;
}

2 个答案:

答案 0 :(得分:2)

fgets将换行符(\n)附加到str

因此,您的str将包含

str = "red green blue blue green blue\n"

因此blueblue\n不匹配,并且将blue\n视为不同的单词。

在您的输出中完全显示出相同的内容

red 1 
green 2 
blue 2 
blue          //see 1 is printed on next line
1

因此按如下所示修剪\n

size_t len = strlen(str);
if (len > 0 && str[len - 1] == '\n')
    str[len - 1] = '\0';

答案 1 :(得分:-1)

#define MAXWORD 100
#define MAXSTRING 10000

void WordCount()
{
    /*Decalaration */
    char *wordArray[MAXWORD] = { 0 };
    int count[MAXWORD] = {0};
    char inputString[MAXSTRING];
    int wordCount=0;
    /*Reading data from input stream*/
    fgets(inputString, sizeof(inputString), stdin);
    /*Remove trailing new line char*/
    inputString[strlen(inputString) -1] = 0;

    /*Init string tokenizer*/
    char *wordPointer = strtok(inputString, " ");
    while (wordPointer)
    {
        int len = strlen(wordPointer);
        int found = 0;
        for (int i = 0; i < wordCount; i++)
        {
            /*check if word already processed then incrment word count*/
            if (strncmp(wordArray[i], wordPointer, len)==0)
            {
                count[i]++;
                found = 1;
                break;
            }

        }
        if (!found)
        {
            /*Allocate memory for string and copy for future comparision*/
            wordArray[wordCount] = (char*)malloc(len + 1);
            strncpy(wordArray[wordCount], wordPointer, len);
            wordArray[wordCount][len] = 0;
            count[wordCount]++;
            wordCount++;
        }
        wordPointer = strtok(NULL, " ");
    }

    /* print words and their frequency*/
    for (int i = 0; i < wordCount; i++)
    {
        printf("%s - %d \n", wordArray[i], count[i]);
    }
}