CCCCCCCCCCCCCCCCCC

练习1-8

#include <stdio.h>
int main(void)
{
    int c, i, nwhite, nother;
    nwhite = nother = 0;
    int ndigit[10] = { 0 };
    while ((c = getchar()) != EOF)
    {
        if (c >= '0' && c <= '9')
            ++ndigit[c - '0'];
        else if (c == ' ' || c == '\n' || c == '\t')
            ++nwhite;
        else
            ++nother;
        printf("digits = ");
        for (i = 0;i<10; ++i) {
            printf("%d", ndigit[i]);
        }
        printf("空格%d,other%d\n", nwhite, nother);
 
    }
    return 0;
}


练习1-13 水平方向

#include <stdio.h>

int main()
{
    int longth, valong = 0;    // 变量 longth 用于存储输入的字符,变量 valong 用于计算单词长度
    int array[20] = { 0 };     // 数组 array 用于记录单词长度为 1-20 的出现次数,初始化为 0

    while ((longth = getchar()) != EOF)   // 循环读取输入
    {
        if (longth != '\n') {   // 如果不是换行符,单词长度加 1
            ++valong;
        }
        else {  // 如果是换行符,记录此长度的单词出现次数,valong 重置为 0
            ++array[valong];
            valong = 0;
        }
    }

    for (int i = 0; i < 20; i++) {  // 循环输出每个单词长度对应的出现次数
        int s = 0;  // 变量 s 用于记录当前长度的单词出现次数
        printf("%02d:", i + 1);  // 输出当前长度
        if (array[i + 1] > 0) {  // 如果有出现次数,输出对应数量的星号
            for (s = 0; s < array[i + 1]; s++) {
                printf("* ");
            }
        }
        if (s > 0)  // 如果有出现次数,输出该数量
            printf("%d", s);
        printf("\n");   // 换行
        s = 0;  // 重置 s 的值
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值