题目:       

    编写一个程序统计输入字符串中:各个数字、空白字符、以及其他所有字符出现的次数。

题目分析:

    这个题目还是挺简单的,对于输入字符串可以利用get进行获取,scanf获取字符串只能获取一行,同时不能获取空格字符,其中需要利用isspace函数计算空白字符的个数,对于获取的字符可以分三种情况,数字、空白字符和其他字符,这个可以利用if语句进行实现。


#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>    //引用isspace函数头文件
 
int main()
{
    int ch = 0;
    int num = 0;
    int space = 0;
    int other = 0;
    while((ch = getchar()) != EOF)   //字符串结束标志,ctrl+z停止获取字符
    {
        if(ch >= '0' && ch <= '9')
        {
            num++;
        }
        else if(isspace(ch))   //isspace函数判断空白字符,包括空格、换行、table等
       {
           space++;
       }
       else
       {
           other++;
       }
    }
    printf("数字num = %d\n", num);
    printf("空白字符space = %d\n", space);
    printf("其他字符other = %d\n", other);
system("pause");
return 0;
}


上面程序中用到了isspace函数,下面简单说明一下其用法:

   头文件:#include <ctype.h>

   定义函数:int isspace(int c);
 
   函数说明:检查参数ch是否为空白字符,也就是判断其是否为空格(' ')、定位字符(' \t ')、CR(' \r ')、换行(' \n ')、垂直定位字符(' \v ')或翻页(' \f ')的情况。


   返回值:若参数c 为空白字符,则返回非 0,否则返回 0。