编写一个程序,把输入作为字符流读取,直至遇到EOF。令其报告每个单词的平均字母数。不要将空白符记为单词中的字母。实际上,标点符号也不应该计算,但现在不比考虑这一点。
#include <stdio.h>
#include <ctype.h>
#include <windows.h>
int inword(char ch);
int main(void)
{
int sum = 0;
int si = 0;
char ch;
while ((ch = getchar()) != EOF) {
if (inword(ch)) sum++;
if (inword(ch) == 1) {
do {
si++;
ch = getchar();
} while (inword(ch));
}
}
printf("共计%d个单词,%d个有效字符,平均每个单词%d个字母.\n", sum, si, si / sum);
system("pause");
return 0;
}
int inword(char ch) {
if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
return 1;
return 0;
}