C primer plus 第八章 练习4:

编写一个程序,把输入作为字符流读取,直至遇到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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值