/*4.编写一个程序,把输入作为字符流读取,直至遇到 EOF。令其报告每个单词的平均字母数。不要将空白字符记为单词中的字母。实际上,标点符号也不应该计算,但现在不必考虑这一点(如果您想做得
好一些,可以考虑使用 ctype.h 系列中的 ispunct()函数)。*/
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#define bool int
#define true 1
#define false 0
int main()
{
char a;
float n_words = 0;
float n_letters = 0;
float b;
bool inword = false;
printf("Please input charecter:");
while ((a = getchar()) != EOF)
{
if (!isspace(a) && !ispunct(a)) //除去空白字符和标点字符
n_letters++;
if (!isspace(a) && !inword)
{
inword = true;//开始一个新单词
n_words++;
}
if (isspace(a) && inword)
inword = false;//到达单词的尾部
}
printf("%.1fcharacters %.1fwords average is %.2f\n", n_letters, n_words, b = (n_letters / n_words));
system("pause");
return 0;
}
C Primer Plus8-4
计算平均单词长度
最新推荐文章于 2024-09-28 21:40:24 发布
本文介绍了一个简单的C语言程序,该程序能够从标准输入读取字符流,并计算并报告每个单词的平均字母数量,同时过滤掉空白字符和标点符号。
325

被折叠的 条评论
为什么被折叠?



