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

hello world.

文章分类

全部博文(308)

分类: C/C++

2010-08-18 11:46:26

    输入一行文字,找出其中大写字母,小写字母,数字,空格以及其他字符各有多少?
    我们可以在程序中设置全局变量,然后顺序读取字符串中的一个字符,根据字符的类型,给统计这种字符类型的个数上加一。直到字符串读取完毕为止。代码如下:
 

#include <stdio.h>
#define N 300
int upper = 0,lower = 0,digit = 0,space = 0,other = 0;

void stat_string(char *);
int main(int argc, char *argv[])
{
    char ch[N],*p_ch;
    p_ch = ch;
    printf("please input a string:\n");
    gets(ch);
    stat_string(p_ch);
    printf("the string :'%s'\nupper = %d , lower = %d , digit = %d , space = %d , other = %d \n"
               ,p_ch,upper,lower,digit,space,other);
    system("pause");
    return 0;
}

void stat_string(char *string)
{
     char c;
     while (c = *string ++)
     {
           if (c >= 'A' && c <= 'Z')
           {
              upper ++;
           }
           else if (c >= 'a' && c <= 'z')
           {
                lower ++;
           }
           else if (c >= '0' && c <= '9')
           {
                digit ++;
           }
           else if (c == ' ')
           {
                space ++;
           }
           else
           {
               other ++;
           }
     }
}


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