极客 - 博文17 - 字符串中各种字符的统计

来源:互联网 发布:金山 阿里 腾讯 视频云 编辑:程序博客网 时间:2024/06/05 19:53
*程序名称: 字符串中各种字符的统计
*作者: 田鑫  
*完成日期: 2016 - 09 - 20  
*版本号: v1.0 
*平台: code blocks 
*问题描述: 统计一个字符串中的数字 字母 空白和其它字符
*解决方法: 
#include <stdio.h>int main(void){    char c;    /*通常计数器都赋值为0*/    int space = 0;    int character = 0;    int digit = 0;    int other = 0;    while((c = getchar()) != '\n') /*回车最为结束条件*/    {        if((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))        {            character++;        }        else if(c >= '0'  && c <= '9')        {            digit++;        }        else if(c == ' ')        {            space++;        }        else        {            other++;        }    }    printf("character = %d\ndigit = %d\nspace = %d\nother = %d\n",                                    character, digit, space, other);    return 0;}

运行结果


0 0
原创粉丝点击