#include <stdio.h>
#include <string.h>
static const char *below20[] = {
"", "one", "two", "three", "four", "five", "six", "seven",
"eight", "nine", "ten", "eleven", "twelve", "thirteen",
"fourteen", "fifteen", "sixteen", "seventeen", "eighteen",
"nineteen"
};
static const char *tens[] = {
"", "", "twenty", "thirty", "forty", "fifty",
"sixty", "seventy", "eighty", "ninety"
};
void append(char *buf, const char *word) {
if (word && *word) {
strcat(buf, word);
strcat(buf, " ");
}
}
void setBelow20AndTens(int num, char *buf) {
if (num == 0) return;
if (num < 20) {
append(buf, below20[num]);
} else if (num < 100) {
append(buf, tens[num / 10]);
setBelow20AndTens(num % 10, buf);
} else {
append(buf, below20[num / 100]);
append(buf, "hundred");
setBelow20AndTens(num % 100, buf);
}
}
void numberToWords(int num, char *buf) {
buf[0] = '\0'; // clear buffer
if (num == 0) {
strcpy(buf, "zero");
return;
}
if (num >= 1000000000) {
setBelow20AndTens(num / 1000000000, buf);
append(buf, "billion");
num %= 1000000000;
}
if (num >= 1000000) {
setBelow20AndTens(num / 1000000, buf);
append(buf, "million");
num %= 1000000;
}
if (num >= 1000) {
setBelow20AndTens(num / 1000, buf);
append(buf, "thousand");
num %= 1000;
}
if (num > 0) {
setBelow20AndTens(num, buf);
}
// Trim trailing space
int len = strlen(buf);
if (len > 0 && buf[len - 1] == ' ')
buf[len - 1] = '\0';
}
int main(void) {
int n = 176283;
char buffer[256] = "";
numberToWords(n, buffer);
printf("%s\n", buffer);
return 0;
}
/*
run:
one hundred seventy six thousand two hundred eighty three
*/