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;
}
温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答