/home/toolbox/public_html/solutions/7/739/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 <stdint.h>
7 #include <math.h>
8 #include <stdlib.h>
9 #include <ctype.h>
10
11 #define TRUE (1 == 1)
12 #define FALSE (1 != 1)
13
14 #define DEBUG if (FALSE)
15
16 #define MAX_LINE 257
17
18 /*
19 * Author: Isaac Traxler
20 * Date: 2012-08-26
21 * Purpose: fun
22 * Problem: 739 - Soundex Indexing
23 */
24
25 /*
26 * This template reads lines of data at a time until end of file.
27 */
28
29 char line[MAX_LINE];
30 char ans[MAX_LINE];
31 int idx;
32
33 void init()
34 {
35 /* FUNCTION init */
36 } /* FUNCTION init */
37
38 void dump()
39 {
40 /* FUNCTION dump */
41 } /* FUNCTION dump */
42
43 int getInput()
44 {
45 /* FUNCTION getInput */
46 int dataReadFlag;
47
48 fgets(line, MAX_LINE, stdin);
49 if (feof(stdin))
50 dataReadFlag = FALSE;
51 else
52 {
53 /* something to read */
54 dataReadFlag = TRUE;
55 line[strlen(line)-1] = 0;
56 } /* something to read */
57 return (dataReadFlag);
58 } /* FUNCTION getInput */
59
60
61 char xlate(char z)
62 {
63 /* FUNCTION xlate */
64 char toReturn = '0';
65
66 /**************
67 * 1 B, P, F, V
68 * 2 C, S, K, G, J, Q, X, Z
69 * 3 D, T
70 * 4 L
71 * 5 M, N
72 * 6 R
73 */
74 switch (z)
75 {
76 /* switch */
77 case 'B':
78 case 'P':
79 case 'F':
80 case 'V':
81 toReturn = '1';
82 break;
83 case 'C':
84 case 'S':
85 case 'K':
86 case 'G':
87 case 'J':
88 case 'Q':
89 case 'X':
90 case 'Z':
91 toReturn = '2';
92 break;
93 case 'D':
94 case 'T':
95 toReturn = '3';
96 break;
97 case 'L':
98 toReturn = '4';
99 break;
100 case 'M':
101 case 'N':
102 toReturn = '5';
103 break;
104 case 'R':
105 toReturn = '6';
106 break;
107 } /* switch */
108 return toReturn;
109 } /* FUNCTION xlate */
110
111 void encode()
112 {
113 /* FUNCTION encode */
114 int i;
115 int slen;
116 char prev;
117 char cur;
118
119 ans[0] = line[0];
120 idx=1;
121 slen = strlen(line);
122 prev = xlate(line[0]);
123 for (i=1; (i<slen) && (4>idx); i++)
124 {
125 /* step along line */
126 cur = xlate(line[i]);
127 DEBUG printf("(line:%s) (cur %c) (idx %d) (i %d)\n", line, cur, idx, i);
128 if (('0' != cur) && (cur != prev))
129 {
130 /* got a value to encode */
131 ans[idx] = cur;
132 idx++;
133 } /* got a value to encode */
134 prev = cur;
135 } /* step along line */
136 while (4>idx)
137 {
138 /* while */
139 ans[idx] = '0';
140 idx++;
141 } /* while */
142 ans[4] = 0;
143 } /* FUNCTION encode */
144
145 void process()
146 {
147 /* FUNCTION process */
148 DEBUG printf("(line [%s])\n", line);
149 encode();
150 printf(" %-25s%s\n", line, ans);
151 } /* FUNCTION process */
152
153 int main()
154 {
155 /* main */
156 int moreToDo;
157
158 init();
159 moreToDo = getInput();
160 printf(" %-25s%s\n", "NAME", "SOUNDEX CODE");
161 while (moreToDo)
162 {
163 /* while */
164 process();
165 moreToDo = getInput();
166 } /* while */
167 printf("%19sEND OF OUTPUT\n", " ");
168 return EXIT_SUCCESS;
169 } /* main */
170