文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>C程序练习-计算字符类型个数

C程序练习-计算字符类型个数

时间:2010-07-26  来源:chengxiaopeng

    题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
    其中这道到,我们用getchar()函数去获取这个字符,然后根据条件进行相关类型字符的区分。代码如下:

#include <stdio.h>

int main(int argc,int *argv[])
{
    char c;
    int letter = 0, space = 0, digit = 0, other = 0;
    printf("please input some characters\n");
    while ((c = getchar()) != '\n')
    {
          if(c >= 'a' && c <='z' || c >='A' && c <='Z')
          {
               letter ++;
          }
          else if (c == ' ')
          {
               space ++;
          }
          else if(c >= '0' && c <='9')
          {
               digit ++;
          }
          else
          {
              other ++;
          }
    }
    
    printf("the char = %d,digit = %d,space = %d,other = %d\n",letter,digit,space,other);
    system("pause");
    return 0;
}


相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载