Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2509521
  • 博文数量: 308
  • 博客积分: 5547
  • 博客等级: 大校
  • 技术积分: 3782
  • 用 户 组: 普通用户
  • 注册时间: 2009-11-24 09:47
个人简介

hello world.

文章分类

全部博文(308)

分类: C/C++

2010-07-26 11:43:50

    题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
    其中这道到,我们用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;
}


阅读(625) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~