c
1#include <stdio.h>
2#include <ctype.h>
3
4int main(void)
5{
6 //
7 int cnt[128] = {0};
8 char buf[256];
9
10 while (fgets(buf, sizeof buf, stdin) != NULL) {
11 for (size_t pos = 0; buf[pos] != '\0'; pos++) {
12 int ch = buf[pos];
13 if (isalpha(ch)) {
14 ch = toupper(ch);
15 cnt[ch]++;
16 }
17 }
18 }
19 //
20 int sum = 0;
21 for (int i = 'A'; i <= 'Z'; i++) {
22 sum += cnt[i];
23 }
24 //
25 for (int i = 'A'; i <= 'Z'; i++) {
26 printf("%c= %3d(%8.3f%%)\n", i, cnt[i], (double)cnt[i] / sum * 100.0);
27 }
28 //
29 return 0;
30}
結果
text
1usr ~/Project/test % ./a.out
2So she was considering in her own mind (as well as she could, for the
3day made her feel very sleepy and stupid), whether the pleasure of
4making a daisy-chain would be worth the trouble of getting up and
5picking the daisies, when suddenly a White Rabbit with pink eyes ran
6close by her.
7A= 16( 7.111%)
8B= 5( 2.222%)
9C= 5( 2.222%)
10D= 13( 5.778%)
11E= 31( 13.778%)
12F= 4( 1.778%)
13G= 5( 2.222%)
14H= 16( 7.111%)
15I= 17( 7.556%)
16J= 0( 0.000%)
17K= 3( 1.333%)
18L= 10( 4.444%)
19M= 3( 1.333%)
20N= 15( 6.667%)
21O= 11( 4.889%)
22P= 6( 2.667%)
23Q= 0( 0.000%)
24R= 12( 5.333%)
25S= 16( 7.111%)
26T= 13( 5.778%)
27U= 7( 3.111%)
28V= 1( 0.444%)
29W= 9( 4.000%)
30X= 0( 0.000%)
31Y= 7( 3.111%)
32Z= 0( 0.000%)
33usr ~/Project/test %
34