zoukankan      html  css  js  c++  java
  • 浙大版《C语言程序设计(第3版)》题目集 练习3-4 统计字符 (15 分)

    练习3-4 统计字符 (15 分)

    本题要求编写程序,输入10个字符,统计其中英文字母、空格或回车、数字字符和其他字符的个数。

    输入格式:

    输入为10个字符。最后一个回车表示输入结束,不算在内。

    输出格式:

    在一行内按照

    letter = 英文字母个数, blank = 空格或回车个数, digit = 数字字符个数, other = 其他字符个数
    

    的格式输出。

    输入样例:

    aZ &
    09 Az
    

    输出样例:

    letter = 4, blank = 3, digit = 2, other = 1

    思路:根据ASCII码确定统计各类的范围。
    char ch;    
    数字范围:0~9 (ch>=48&&ch<=57) 或 (ch>='0'&&ch<='9')
    小写字母:a~z (ch>=97&&ch<=122)或 (ch>='a'&&ch<='z')
    大写字母:A~Z (ch>=65&&ch<=90) 或 (ch>='A'&&ch<='Z')
    空格:space (ch==32) 或 (ch==' ')
    回车:enter (ch==10) 或 (ch==' ')

    ASCII码表:

    代码如下:
    #include<stdio.h>
    int main()
    {
        int i, letter, digit, other, blank;
        char ch;
    	
        digit=0,letter=0,other=0;
        for(i=0;i<10;i++)
        {
    	ch=getchar();
    	if ((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z'))
    		letter++;
    	else if(ch>='0'&&ch<='9')
    		digit++;
    	else if(ch==' '||ch=='
    ')
    		blank++;
    	else
    		other++;
        }
        printf("letter = %d, blank = %d, digit = %d, other = %d",letter,blank,digit,other);
    	
        return 0;
    }
    

      


  • 相关阅读:
    Ext.dataGroupingStore/JsonStore/SimpleStore
    转:LinQ操作汇总(From CSharpSamples)
    XSLT教程 比较全的
    使用ASP.Net Forms模式实现WebService身份验证
    关于DataRow的RowState和RowVersion
    C#日志工具汇总
    转 Using log4net,
    js//初始话日期
    两个数据库表的连接 查询
    ExtJS入门之三 查询
  • 原文地址:https://www.cnblogs.com/IT-Lead-The-World/p/10349676.html
Copyright © 2011-2022 走看看