【C语言】英文文章出现次数最多的单词

问题描述:
在当前目录中存在文件名为“case14.in”的文本文件,其内容为一篇英文文章(以EOF作为结束标志)。现要求读取该文本文件内容,统计文章中每个单词出现的次数,并输出出现次数最多的前5个单词及其出现次数(按出现次数由多到少的顺序输出,次数相同时按字典顺序输出,不足5个单词时,按顺序输出全部单词)。程序中注意如下细节:
(1)空格、标点符号与回车符起到分隔单词的作用。
(2)文章一行的末尾可能有连字符,出现连字符时,该行最末的字符串与下行最先出现的字符串构成一个单词;
(3)名词缩写算一个单词;
(4)数字不算单词;
(5)单词不区分大小写;
(6)输出时单词全是用小写。

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

struct WORDCOUNT{
	char word[20];
	int count;
};

int main()
{
	FILE *fp;
	struct WORDCOUNT wordcount[10000];
	char essay[200000], ch, temp[20];
	int i, j, k, len, count, t;
	
	fp = fopen("case14.in", "r");
	for(i=0; (ch = fgetc(fp)) != EOF; i++)
		essay[i] = ch;
	len = i;
	
	//全部字符写到essay数组中 
	for(i=0; i<len; i++)
	{
		if(essay[i]=='-')
		{
			for(j=i+1; j<len; j++)
				essay[j-1] = essay[j];
			len--;
		}	
		else if(essay[i]>='A' && essay[i]<='Z')
			essay[i] +=32;
		else if(essay[i]>='a' && essay[i]<='z')
			essay[i]= essay[i];
		else essay[i] = ' ';
	}
	
	
	//删除多余空格 
	for(i=1; i<len; i++)
	{
		if(essay[i-1]==' ' && essay[i]==' ')
		{
			for(j=i+1; j<len; j++)
				essay[j-1] = essay[j];
			len--;
		}
	}
	
	//分词 
	k=0;
	for(i=0; i<10000, k<len; i++)
	{
		for(j=0; j<20 && essay[k]!=' ' && k<len; j++, k++)
			wordcount[i].word[j] = essay[k];
		k++;
	}
	count = i;
	
	//赋值  
	for(i=0; i<count; i++)
		wordcount[i].count = 1;
	
	//计算单词数 
	for(i=0; i<count; i++)
	{
		for(j=i+1; j<count; j++)
		{
			if(strcmp(wordcount[i].word, wordcount[j].word)==0)
			{
				wordcount[i].count += wordcount[j].count;
				for(k=j+1; k<count; k++)
					strcpy(wordcount[k-1].word, wordcount[k].word);
				count--;
			}
		}
	}
	
	//按单词排序
	for(i=0; i<count-1; i++)
	{
		for(j=1; j<count-i; j++)
		{
			if(strcmp(wordcount[j-1].word, wordcount[j].word) > 0)
			{
				strcpy(temp, wordcount[j-1].word);
				strcpy(wordcount[j-1].word, wordcount[j].word);
				strcpy(wordcount[j].word, temp);
				t = wordcount[j-1].count;
				wordcount[j-1].count = wordcount[j].count;
				wordcount[j].count = t;
			}
		}
	}
	
	//按数量排序 
	for(i=0; i<count-1; i++)
	{
		for(j=1; j<count-i; j++)
		{
			if(wordcount[j-1].count < wordcount[j].count)
			{
				strcpy(temp, wordcount[j-1].word);
				strcpy(wordcount[j-1].word, wordcount[j].word);
				strcpy(wordcount[j].word, temp);
				t = wordcount[j-1].count;
				wordcount[j-1].count = wordcount[j].count;
				wordcount[j].count = t;
			}
		}
	}
	
	if(count<5)
		for(i=0; i<count; i++)
			printf("%s %d\n", wordcount[i].word, wordcount[i].count);
	else
		for(i=0; i<5; i++)
			printf("%s %d\n", wordcount[i].word, wordcount[i].count);
	
}
  • 14
    点赞
  • 73
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值