文章详情

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

C程序习题-统计字符类型个数[10.8]

时间:2010-08-18  来源:chengxiaopeng

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

#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 ++;
           }
     }
}


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

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载