多语言展示
当前在线:368今日阅读:84今日分享:32

用C语言编写,统计各种字符个数

在visual C++ 6.0上,用C语言编写,统计各种字符个数
工具/原料

visual C++ 6.0

方法/步骤
1

打开visual C++ 6.0-文件-新建-文件-C++ Source File

2

定义变量:#includemain(){    char c;                                     /*定义c为字符型*/    int letters = 0, space = 0, digit = 0, others = 0;    /*定义letters、space、digit、others、四个变量为基本整型*/

3

输入字符:    printf('please input some characters\n');    while ((c = getchar()) != '\n')                      /*当输入的不是回车时执行while循环体部分*/

4

判断是否是英文字母:        if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z')            letters++;                              /*当输入的是英文字母时变量letters加1*/

5

判断是否是空格:        else            if (c == ' ')                space++;                            /*当输入的是空格时变量space加1*/

6

判断是否是数字:        else            if (c >= '0' && c <= '9')                digit++;                            /*当输入的是数字时变量digit加1*/

7

判断是否是其它字符:        else            others++;                           /*当输入的即不是英文字母又不是空格或数字是变量others加1*/

8

输出结果:    printf('char=%d space=%d digit=%d others=%d\n',letters,space,digit,others);    /*将最终统计结果输出*/

9

完整的源代码:#includemain(){    char c;                                     /*定义c为字符型*/    int letters = 0, space = 0, digit = 0, others = 0;    /*定义letters、space、digit、others、四个变量为基本整型*/    printf('please input some characters\n');    while ((c = getchar()) != '\n')                      /*当输入的不是回车时执行while循环体部分*/    {        if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z')            letters++;                              /*当输入的是英文字母时变量letters加1*/        else            if (c == ' ')                space++;                            /*当输入的是空格时变量space加1*/        else            if (c >= '0' && c <= '9')                digit++;                            /*当输入的是数字时变量digit加1*/        else            others++;                           /*当输入的即不是英文字母又不是空格或数字是变量others加1*/    }    printf('char=%d space=%d digit=%d others=%d\n',letters,space,digit,others);    /*将最终统计结果输出*/}

推荐信息