统计一篇英文文章的英文单词个数

来源:百度知道 编辑:UC知道 时间:2024/05/11 17:30:30
用C语言编写

/*
本程序由Turbo C2.0编译通过。英文文章请命名为english.txt并放在Turbo C所在目录下。运行结果以文件方式输出,输出文件result.txt也在Turbo C所在目录下。
word是不同的单词;
count是该单词在文章中出现的次数;
percent是文章中各单词出现的频率。
*/

#include "stdio.h"
main()
{
FILE *fp,*result;
char ch='\0';
char word[1000][20]; /* 最多存1000个不同单词,每个单词在20个字符内。 */
int count_word[1000]={0}; /* 每个单词对应个数 */
int i=0,j=0,k=0,flag=2,total=0;
float percent; /* 每个单词出现频率 */
clrscr();

if(((fp=fopen("english.txt","r"))&&(result=fopen("result.txt","w")))==NULL)
{
printf("Can't open file\n");
printf("Press any key to exit...");
getch();
exit(0);
}

printf("\nPlease wait...");
while(!feof(fp))
{
ch=fgetc(fp);
if(ch>='a'&&ch<='z'||ch>='A'&&ch<='Z')
{
i