编写一个函数在终端中用 ./a.out <data.txt 阅读txt文件
其中data.txt文件为
My
name
is
iKun
And
my
favorite
sport
is
basketball
and
if
you
love
me
you
should
become
IKUN
统计一共有多少个不同的单词(大小写算不同的单词)
同时统计出现频率最多的单词是多少次,并且打印出这个单词(如果有多组,那么都打印出来)
编写一个函数在终端中用 ./a.out <data.txt 阅读txt文件
其中data.txt文件为
My
name
is
iKun
And
my
favorite
sport
is
basketball
and
if
you
love
me
you
should
become
IKUN
统计一共有多少个不同的单词(大小写算不同的单词)
同时统计出现频率最多的单词是多少次,并且打印出这个单词(如果有多组,那么都打印出来)
#include <stdio.h>
#include <string.h>
#define MAX_WORD_LENGTH 60
#define MAX_WORD_NUM 200
char words[MAX_WORD_NUM][MAX_WORD_LENGTH] = {0};
int count[MAX_WORD_NUM] = {0};
int main() {
int n = 0, max = 0;
char word[MAX_WORD_LENGTH];
while (scanf("%s", word) == 1) {
int i = 0;
while (i < n && strcmp(words[i], word) != 0)
i++;
if (i == n) {
strcpy(words[i], word);
n++;
}
count[i]++;
if (count[i] > max)
max = count[i];
}
printf("Total number of different words: %d\n", n);
printf("Max Freq: %d\n", max);
for (int i = 0; i < n; i++) {
if (count[i] == max)
printf("%s\n", words[i]);
}
return 0;
}
$ gcc -Wall main.c
$ ./a.out < data.txt
Total number of different words: 17
Max Freq: 2
is
you