0

我正在尝试编写一个程序,它打开一个文本文件,从文件中读取,将大写更改为小写,然后计算该单词在文件中出现的次数并将结果打印到新的文本文件中。

到目前为止,我的代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <ctype.h>
#include <string.h>

int main()
{

    FILE *fileIN;
    FILE *fileOUT;
    char str[255];
    char c;
    int i = 0;

    fileIN = fopen ("input.txt", "r");
    fileOUT = fopen ("output.txt", "w");

    if (fileIN == NULL || fileOUT == NULL)
    {
        printf("Error opening files\n");
    }

    else
    {
        while(! feof(fileIN)) //reading and writing loop
        {
            fscanf(fileIN, "%s", str); //reading file


            i = 0;
            c = str[i];
            if (isupper(c)) //changing any upper case to lower case
            {
                c =(tolower(c));
                str[i] = putchar(c);
            }

            printf("%s ", str); //printing output

                            fprintf(fileOUT, "%s\n", str); //printing into file
        }




        fclose(fileIN);
        fclose(fileOUT);
    }
    getch();
}

input.txt文件包含以下“西班牙的雨主要落在飞机上”不要问为什么。程序运行后的输出如下所示:西班牙的雨主要落在飞机上

我设法将大写单词小写。我现在无法理解如何计算每个单词的出现次数。例如,在输出中,我希望它说“the 2”,意思是 2 出现了,这也意味着我不想再将“the”存储在该文件中。

我在想 strcmp 和 strcpy 但不确定如何以我想要的方式使用它们。

帮助将不胜感激

(如果格式不好,请见谅)

4

2 回答 2

1

您可能想要创建一个哈希表,其中单词作为键,频率作为值。

素描思路:

  • 识别单词,即由空格分隔的字母数字字符串,尝试使用 strtok()
  • 对于每个单词
    • 在基于哈希表的字典中搜索单词
      • 如果找到:增加频率
      • 如果找到:在字典中插入一个新条目为 (word, 1)

最后,打印字典的内容,即所有条目,entry.word以及entry.frequency

有关详细信息,请参见此问答:Quick Way to Implement Dictionary in C它基于圣经“The C Programming Language”的第6.6节

更新基于OP的评论:

哈希表只是一个高效的表,如果你不想使用它,你仍然可以使用 vanilla 表。这里有一些想法。

typedef struct WordFreq {
    char  word[ N ];
    int   freq;
} WordFreq;

WordFreq wordFreqTable[ T ];

(N is the maximum length of a single word, T is the maximum number of unique words)

对于搜索和插入,您可以在表格中进行线性搜索for( int i = 0; i != T; ++i ) {

于 2013-04-04T23:41:04.370 回答
0

简单示例(需要错误捕获、释放内存、排序以供使用 qsort 等...)

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

#define BUFFSIZE 1024

typedef struct _wc {
    char *word;
    int count;
} WordCounter;

WordCounter *WordCounters = NULL;
int WordCounters_size = 0;

void WordCount(char *word){
    static int size = 0;
    WordCounter *p=NULL;
    int i;

    if(NULL==WordCounters){
        size = 4;
        WordCounters = (WordCounter*)calloc(size, sizeof(WordCounter));
    }
    for(i=0;i<WordCounters_size;++i){
        if(0==strcmp(WordCounters[i].word, word)){
            p=WordCounters + i;
            break;
        }
    }
    if(p){
        p->count += 1;
    } else {
        if(WordCounters_size == size){
            size += 4;
            WordCounters = (WordCounter*)realloc(WordCounters, sizeof(WordCounter)*size);
        }
        if(WordCounters_size < size){
            p = WordCounters + WordCounters_size++;
            p->word = strdup(word);
            p->count = 1;
        }
    }
}

int main(void){
    char buff[BUFFSIZE];
    char *wordp;
    int i;

    while(fgets(buff, BUFFSIZE, stdin)){
        strlwr(buff);
        for(wordp=buff; NULL!=(wordp=strtok(wordp, ".,!?\"'#$%&()=@ \t\n\\;:[]/*-+<>"));wordp=NULL){
            if(!isdigit(*wordp) && isalpha(*wordp)){
                WordCount(wordp);
            }
        }
    }
    for(i=0;i<WordCounters_size;++i){
        printf("%s:%d\n", WordCounters[i].word, WordCounters[i].count);
    }

    return 0;
}

演示

>WordCount.exe
The rain in Spain falls mainly in the plane
^Z
the:2
rain:1
in:2
spain:1
falls:1
mainly:1
plane:1
于 2013-04-05T10:54:08.240 回答