一、题目分析
解决字符的分类问题可以通过字符的ASCII码进行判断,因为不同的字符对应不同的ASCII码。
例如常用的有:空格符——32,制表符——9,换行符——10,Ctrl+Z的组合键输入的字符对应的是EOF结束标志。
二、源码
1: #include <string.h>
2: #include <stdio.h>
3:
4: int main()
5: {
6: char c;
7: int nSpace = 0;
8: int nTable = 0;
9: int nEnter = 0;
10:
11: printf("Please input a string:\n");
12: scanf("%c", &c);
13: while (c != EOF)
14: {
15: switch(c)
16: {
17: case 32:
18: nSpace++;
19: break;
20: case 9:
21: nTable++;
22: break;
23: case 10:
24: nEnter++;
25: break;
26: default:
27: break;
28: }
29: scanf("%c", &c);
30: }
31: printf("the number of space:%d\n", nSpace);
32: printf("the number of table:%d\n", nTable);
33: printf("the number of enter:%d\n", nEnter);
34: getchar();
35: return 0;
36: }