1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
#include<stdio.h> int main() { int sz[10]={0},zm[26]={0},z[26]={0},i,space=0,e=0,t=0; char c; printf ( "请输入一段字符,统计其中各字符的数量\n" ); while ((c= getchar ())!= '\n' ) { if (c<= 'z' &&c>= 'a' ) zm[c- 'a' ]++; else if (c<= 'Z' &&c>= 'A' ) z[c- 'A' ]++; else if (c<= '9' &&c>= '0' ) sz[c- '0' ]++; else if (c== ' ' ) space++; else e++; } printf ( "\n\n" ); for (i=0;i<=9;i++) { t++; printf ( " %d的个数为%d " ,i,sz[i]); if (t%3==0) printf ( "\n" ); } t=0; printf ( "\n\n\n" ); for (i=0;i<=25;i++) { t++; printf ( " %c的个数为%d " ,i+97,zm[i]); if (t%3==0) printf ( "\n" ); } t=0; printf ( "\n\n\n" ); for (i=0;i<=25;i++) { t++; printf ( " %c的个数为%d " ,i+65,z[i]); if (t%3==0) printf ( "\n" ); } t=0; printf ( "\n\n\n" ); printf ( " 空格的个数为%d\n\n" ,space); printf ( " 其他字符的个数为%d\n" ,e); return 0; } |
再来一则C语言统计输入字符个数的代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
#include <stdio.h> #define MAXWORD 30 //单词的最大长度 #define IN 1 //在单词内,即没有遇到空格 #define OUT 0 //在单词外,即遇到了空格 /*word length ==> wl单词长度简称*/ int wl() { char c; int i; int nc; int state; //state为单词的状态:IN或OUT; int overflow; //操过MAXWORD的单词个数 int wl[MAXWORD]; //长度为1~30的的字符长度统计 state = OUT; nc = 0; overflow = 0; for (i = 1; i < MAXWORD; ++i) wl[i] = 0; while ((c = getchar ()) != EOF) if (c == ' ' || c == '\n' || c == '\t' ) { state = OUT; if (nc > 0) if (nc < MAXWORD) ++wl[nc]; else ++overflow; nc = 0; } else if (state == OUT) { state = IN; nc = 1; } else ++nc; for (i = 1; i < MAXWORD; ++i) printf ( "长度为:%d的单词个数为:%d : \n\n" , i, wl[i]); return 0; } main() { wl(); } |
以上所述就是本文的全部内容了,希望大家能够喜欢