用c语言编写一个小程序,可以读入一个英文的文本文件,显示这个文件,并统计这个文件有多少个字符,多少

用c语言编写一个小程序,可以读入一个英文的文本文件,显示这个文件,并统计这个文件有多少个字符,多少个单词,多少个空白(空格、TAB),多少个段落,按照单词的长度进行统计。

#include <stdio.h>
#include <string.h>
#include <img alt="搜索" src="https://gss0.bdstatic.com/70cFsjip0QIZ8tyhnq/img/iknow/qb/select-search.png" id="selectsearch-icon"><ctype.h>

int main()
{
FILE *fp = NULL;
char read_buf = 0;
int char_count = 0;
int word_count = 0;
int tab_count = 0;
int blank_count = 0;
int paragraph_count = 0;
int word_start = 0;

fp = fopen("English_file.txt", "r");
if (fp == NULL)
{
printf("Can't open the file.\n");
return -1;
}

printf("The following is the file content: \n");
while (!feof(fp))
{
read_buf = fgetc(fp);
printf("%c", read_buf);
if (isalpha(read_buf))
{
char_count++;//字母
if (word_start == 0)
{
word_start = 1;
}
}
else//非字母
{
if (word_start == 1)
{
word_count++;//单词
word_start = 0;
}
switch (read_buf)
{
case '\t'://tab
tab_count++;
break;
case '\040'://空格
blank_count++;
break;
case '.':
case '!':
case '?':
case ':':
read_buf = fgetc(fp); //再取一个字符
printf("%c", read_buf);
//如果是回车或者换行就表示一个段落结束了
if ( read_buf == '\r' || read_buf == '\n' )
{
paragraph_count++ ; //段落
}
break;
default:
break;
}
}

}
printf("\n");

printf("tab_count: %d\n", tab_count);
printf("blank_count: %d\n", blank_count);
printf("char_count: %d\n", char_count);
printf("word_count: %d\n", word_count);
printf("paragraph_count: %d\n", paragraph_count);

return 0;
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2019-10-23
编写C程序,要求:
1.建立一个文本文件test.dat, 存入你的英文名字;2.打开test.dat文件,在其后加入你的学号;
3.打开lestdat文件,读入文件中的内容,并在屏幕上显示出来。
第2个回答  2016-05-23
这题 30分少了 得写一段时间呢
第3个回答  2018-03-30
为什么我运行结果错了
第4个回答  2016-05-13
我可以有偿代劳
相似回答