70,014
社区成员




/*编写一个程序,在遇到EOF之前,把输入作为字符流读取。
该程序要报告平均每个单词的字母数。不要把空白统计为单词的字母。
实际上,标点符号也不应该统计,但是现在暂时不同考虑这么多
(如果你比较在意这点,考虑使用ctype.h系列中的ispunct()函数)*/
#include <stdio.h>
#include <ctype.h>
#include <stdbool.h>
int main(void)
{
bool inword = false;
int ch, words = 0, letter = 0;
printf("Please enter some words (EOF to quit):\n");
while ((ch = getchar()) != EOF)
{
if (ispunct(ch))
{
continue;
}
if (isalpha(ch))
{
letter++;
}
if (!isspace(ch) && !inword)
{
inword = true;
words++;
}
if (isspace(ch) && inword)
{
inword = false;
}
}
double count = letter / words;
printf("Total words: %d\n", words);
printf("Total letters: %d\n", letter);
printf("Average letters of words: %g\n", count);
return 0;
}