-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfleventyfive.c
More file actions
executable file
·67 lines (60 loc) · 1.9 KB
/
Copy pathfleventyfive.c
File metadata and controls
executable file
·67 lines (60 loc) · 1.9 KB
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
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*
* http://www.reddit.com/r/dailyprogrammer/comments/34rxkc/20150504_challenge_213_easy_pronouncing_hex/
*/
int charToHex(char kar);
int main(int argc, char** argv) {
const char* ONES[] = {"", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "a", "bee", "cee", "dee", "e", "eff"};
const char* TENS[] = {"", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety", "atta", "bibbity", "city", "dickety", "ebbity", "fleventy"};
const char* TEENS[] = {"ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen", "ahteen", "beeteen", "ceeteen", "deeteen", "eeteen", "effteen"};
const char* BITEY = " bitey ";
char* itr = argv[1];
int size = strlen(argv[1]), isteen, bitey = 0;
char output[255]; //too lazy for memory management
char* outitr = output;
if (itr[0] == '0' && tolower(itr[1]) == 'x') {
itr += 2;
size -= 2;
}
while (size > 0) {
if (size % 2) { // low nibble
if (isteen) {
strcpy(outitr, TEENS[charToHex(*itr)]);
outitr += strlen(TEENS[charToHex(*itr)]);
isteen = 0;
} else {
strcpy(outitr, ONES[charToHex(*itr)]);
outitr += strlen(ONES[charToHex(*itr)]);
}
if (size > 1) {
strcpy(outitr, BITEY);
outitr += strlen(BITEY);
}
} else { //high nibble
if (charToHex(*itr) != 1) {
strcpy(outitr, TENS[charToHex(*itr)]);
outitr += strlen(TENS[charToHex(*itr)]);
} else {
isteen = 1;
}
if (!isteen && *(itr + 1) != '0') {
*outitr = '-';
outitr++;
}
}
size--;
itr++;
}
printf("%s", output);
}
int charToHex(char kar) {
int output;
if (!sscanf(&kar, "%x", &output)) {
fprintf(stderr, "oh no, tried to convert a nonhex char");
exit(EXIT_FAILURE);
}
return output;
}