如何在C语言中获取和显示单词的密度和出现?

时间:2013-07-02 02:12:11

标签: c string stdio string.h

我目前正在处理一个文本文件,其中包含固定数量的单词。我想要的只是计算文本文件中单词的出现次数并输出其密度。我在文本文件中有266个单词,我想输出单词的数量和密度以及单词本身。

e.g. (sample.txt)
The quick brown fox jumps over the lazy brown dog.

Output:
**Count     Density     Word**
   2         0.2%       The
   2         0.2%       brown

OP的代码:

#define DELIM " "
#include <stdio.h>

int main()
{
    int c; 
    int count = 0;
    FILE *file, *temp;
    char line[200];
    char *result, *result2;
    file = fopen("sample.txt", "r"); 
    temp = fopen("temp.txt", "w"); 

    if (file)
    { 
        while ((c = getc(file)) != EOF) 
        { 
            if (c == '.' || c == '(' || c == ')' || c == ',' || c == ':' || c == '-' || c == '’')
            {
                fputc(putchar(' '), temp);
                continue;
            } else
            {
                count = count + 1;
                fputc(c, temp);
            }
        }
        fclose(file); 
        fclose(temp); 
        temp = fopen("temp.txt", "r"); 
        while (fgets(line,200,temp) != NULL)
        {

1 个答案:

答案 0 :(得分:3)

  1. 使用搜索优化的数据结构,例如二进制树或哈希表,由单词索引;
  2. 由于您不区分大小写,请在存储之前将单词转换为小写或大写;
  3. 在每个节点上,存储计数;
  4. 添加新单词时,其计数为1;
  5. 添加现有单词时,会将其计数增加1;
  6. 处理任何单词时,请增加全局计数器。
  7. 现在,您可以遍历树或哈希表并输出字数。同时,您可以通过将字数除以全局计数来输出密度。