/home/toolbox/public_html/solutions/100/10008/a.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <sys/types.h>
4 #include <sys/stat.h>
5 #include <fcntl.h>
6 #include <stdlib.h>
7 #include <math.h>
8 #include <stdint.h>
9
10 #define TRUE (1 == 1)
11 #define FALSE (1 != 1)
12
13 #define DEBUG if (FALSE)
14
15 /* fprintf(stderr, "functionName: message", varslist); */
16
17 /*
18 * Author: Isaac Traxler
19 * Date: 2016-09-14
20 * Purpose: fun
21 * Problem: 10008 - What’s Cryptanalysis?
22 */
23
24 /*
25 * This template reads data a specified number of times.
26 */
27
28 #define MAX_LINE 2049
29 #define LETTERS 26
30
31 typedef struct
32 {
33 /* struct REC_TYPE */
34 char let;
35 int cnt;
36 } /* struct REC_TYPE */ REC_TYPE;
37
38 int numberOfTimes;
39 char line[MAX_LINE];
40 REC_TYPE counts[LETTERS];
41
42 int compare(const void * a, const void * b)
43 {
44 /* FUNCTION compare */
45 REC_TYPE *A = (REC_TYPE *)a;
46 REC_TYPE *B = (REC_TYPE *)b;
47 return ( B->cnt - A->cnt );
48 } /* FUNCTION compare */
49
50 void init()
51 {
52 /* FUNCTION init */
53 int i;
54
55 for (i=0; LETTERS>i; i++)
56 {
57 /* for each letter */
58 counts[i].let = 'A' + i;
59 counts[i].cnt = 0;
60 } /* for each letter */
61
62 scanf("%d ", &numberOfTimes);
63 } /* FUNCTION init */
64
65 void dump()
66 {
67 /* FUNCTION dump */
68 int i;
69
70 for (i=0; LETTERS>i; i++)
71 {
72 /* for each letter */
73 if (0 < counts[i].cnt)
74 {
75 printf("%c %d\n", counts[i].let, counts[i].cnt);
76 }
77 } /* for each letter */
78 } /* FUNCTION dump */
79
80 void getInput()
81 {
82 /* FUNCTION getInput */
83 fgets(line, MAX_LINE, stdin);
84 line[strlen(line)-1] = 0;
85 } /* FUNCTION getInput */
86
87 void process()
88 {
89 /* FUNCTION process */
90 int i;
91 int tmp;
92
93 for (i=0; i<strlen(line); i++)
94 {
95 /* for each character */
96 if (isalpha(line[i]))
97 {
98 /* found a letter */
99 tmp = toupper(line[i]) - 'A';
100 counts[tmp].cnt = counts[tmp].cnt + 1;
101 } /* found a letter */
102 } /* for each character */
103 } /* FUNCTION process */
104
105 int main()
106 {
107 /* main */
108 int i;
109
110 init();
111 for (i=0; i<numberOfTimes; i++)
112 {
113 /* while */
114 getInput();
115 process();
116 } /* while */
117 qsort(counts, LETTERS, sizeof(REC_TYPE), compare);
118 dump();
119 return EXIT_SUCCESS;
120 } /* main */
121
122