C Primer Plus8-4

本文介绍了一个简单的C语言程序,该程序能够从标准输入读取字符流,并计算并报告每个单词的平均字母数量,同时过滤掉空白字符和标点符号。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/*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;
 }

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值