-
Notifications
You must be signed in to change notification settings - Fork 5
/
number_to_words_and_letters_count.c
240 lines (195 loc) · 5.59 KB
/
number_to_words_and_letters_count.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/**
* Program to convert number to words and Number letters count.
* https://projecteuler.net/problem=17
*/
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <assert.h>
#include <stdbool.h>
#define MAX_LETTERS_LENGTH 100
const char LETTERS[20][10] = {
"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten",
"Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen",
"Eighteen", "Nineteen", "Twenty"
};
/**
* Maps number to respective text.
*
* @param int num
*
* @return string
*/
char *mapper(int num)
{
if (num == 0) {
return "Zero";
}
if (num <= 20) {
return (char *) LETTERS[num - 1];
}
switch (num) {
case 30:
return "Thirty";
case 40:
return "Forty";
case 50:
return "Fifty";
case 60:
return "Sixty";
case 70:
return "Seventy";
case 80:
return "Eighty";
case 90:
return "Ninety";
case 100:
return "Hundred";
case 1000:
return "Thousand";
default:
return "";
}
}
/**
* Determines if we should add 'and'.
*
* @param int* arr
* @param int j
*
* @return bool
*/
bool should_add_conjunction(int *arr, int j)
{
bool flag = false;
for (int x = j - 1; x >= 0; x--) {
if (arr[x] != 0) {
flag = true;
break;
}
}
return flag;
}
/**
* Reduce number to array.
*
* @param int* arr
* @param int num
*
* @return int
*/
int num_to_array(int *arr, int num)
{
int digit, i = 0;
while (num > 0) {
digit = num % 10;
arr[i++] = digit;
num /= 10;
}
return i;
}
/**
* Convert number to words.
*
* @param int num
* @param string separator
*
* @return string
*/
char *number_to_words(int num, char *separator)
{
if (num < 0 || num > 9999) {
return "Number out of range";
}
if (num <= 20) {
return mapper(num);
}
char *word = (char *) malloc(MAX_LETTERS_LENGTH * sizeof(char));
int place, arr[5], i = num_to_array(arr, num);
for (int j = i - 1; j >= 0; j--) {
if (arr[j] == 0) {
continue;
}
place = (int) pow(10, j);
if (place <= 10) {
if (j == 0) {
snprintf(word, MAX_LETTERS_LENGTH, "%s%s", word, mapper(arr[j]));
break;
}
int tens = arr[j] * place;
int ones = arr[j - 1];
if ((tens + ones) <= 20) {
snprintf(word, MAX_LETTERS_LENGTH, "%s%s", word, mapper(tens + ones));
} else if (ones != 0) {
snprintf(word, MAX_LETTERS_LENGTH, "%s%s%s%s", word, mapper(tens), separator, mapper(ones));
} else {
snprintf(word, MAX_LETTERS_LENGTH, "%s%s", word, mapper(tens));
}
--j;
} else {
snprintf(word, MAX_LETTERS_LENGTH, "%s%s%s%s", word, mapper(arr[j]), separator, mapper(place));
if (num == arr[j] * place) {
return word;
}
snprintf(word, MAX_LETTERS_LENGTH, "%s%s", word, separator);
if (place == 100 && should_add_conjunction(arr, j)) {
snprintf(word, MAX_LETTERS_LENGTH, "%s%s%s", word, "and", separator);
}
}
}
return word;
}
/**
* Count number of letters in given range.
*
* @param int start
* @param int end
* @param string separator
*
* @return int
*/
int range_sum(int start, int end, char *separator)
{
int sum = 0;
char *word;
for (int i = start; i <= end; i++) {
word = number_to_words(i, separator);
sum += strlen(word);
}
return sum;
}
int main()
{
int test_data[20] = {
1, 10, 15, 35, 83, 100, 110, 117, 155, 223, 577, 999,
1000, 1100, 1222, 5000, 5555, 7654, 70004, 9999
};
char *test_string[20] = {
"One", "Ten", "Fifteen", "Thirty Five", "Eighty Three", "One Hundred", "One Hundred and Ten",
"One Hundred and Seventeen", "One Hundred and Fifty Five", "Two Hundred and Twenty Three",
"Five Hundred and Seventy Seven", "Nine Hundred and Ninety Nine", "One Thousand", "One Thousand One Hundred",
"One Thousand Two Hundred and Twenty Two", "Five Thousand", "Five Thousand Five Hundred and Fifty Five",
"Seven Thousand Six Hundred and Fifty Four", "Seven Thousand and Four",
"Nine Thousand Nine Hundred and Ninety Nine"
};
char *test_string_2[20] = {
"One", "Ten", "Fifteen", "Thirty-Five", "Eighty-Three", "One-Hundred", "One-Hundred-and-Ten",
"One-Hundred-and-Seventeen", "One-Hundred-and-Fifty-Five", "Two-Hundred-and-Twenty-Three",
"Five-Hundred-and-Seventy-Seven", "Nine-Hundred-and-Ninety-Nine", "One-Thousand", "One-Thousand-One-Hundred",
"One-Thousand-Two-Hundred-and-Twenty-Two", "Five-Thousand", "Five-Thousand-Five-Hundred-and-Fifty-Five",
"Seven-Thousand-Six-Hundred-and-Fifty-Four", "Seven-Thousand-and-Four",
"Nine-Thousand-Nine-Hundred-and-Ninety-Nine"
};
for (int i = 0; i < 13; i++) {
assert(strcmp(test_string[i], number_to_words(test_data[i], " ")) == 0);
assert(strcmp(test_string_2[i], number_to_words(test_data[i], "-")) == 0);
}
assert(36 == range_sum(1, 9, ""));
assert(70 == range_sum(10, 19, ""));
assert(748 == range_sum(20, 99, ""));
assert(20259 == range_sum(100, 999, ""));
assert(21124 == range_sum(1, 1000, ""));
assert(152577 == range_sum(1, 5000, ""));
return 0;
}