This is quite simple.
If all you want is [0-99] then all you need is the
GetHundredsWords
below, but this code will print any number you can put in an integer.
For every order of 1000, the numbering is the same, for example: 123 is
one hundred twenty three
and 123,000 is
one hundred twenty three thousand
, so we only really need code which can go up to 1000 (
GetHundredsWords()
), then just do that for every order of 1000, adding in the correct magnitude (
GetWord()
)
#include <stdio.h>
static size_t GetHundredsWords(char *buffer, int n, bool &bAppended) {
#define APPEND(fmt, ...) \
if (bAppended) { \
*ptr++ = ' '; \
} else { \
bAppended = true; \
} \
ptr += sprintf(ptr, fmt, __VA_ARGS__);
const char *szOnesWords[] = { "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
const char *szTeenWords[] = { "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
const char *szTensWords[] = { "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" };
char *ptr = buffer;
if (n >= 100) {
APPEND("%s hundred", szOnesWords[n / 100 - 1]);
n %= 100;
}
if (n >= 20) {
APPEND(szTensWords[n / 10 - 2]);
n %= 10;
}
if (n >= 10) {
APPEND(szTeenWords[n - 10]);
} else if (n > 0) {
APPEND(szOnesWords[n - 1]);
}
return ptr - buffer;
}
char *GetWord(char *buffer, int n) {
const char *szMagnitudes[] = { " billion", " million", " thousand", "" };
char *ptr = buffer;
if (n == 0) {
return "zero"; }
if (n < 0) { ptr += sprintf(ptr, "negative ");
n *= -1;
}
bool bAppended = false;
int nMagnitude = 1000 * 1000 * 1000; for (int i = 0; nMagnitude > 0; ++i) {
if (n >= nMagnitude) {
ptr += GetHundredsWords(ptr, n / nMagnitude, bAppended);
ptr += sprintf(ptr, szMagnitudes[i]);
n %= nMagnitude;
}
nMagnitude /= 1000;
}
return buffer;
}
#define TEST(n) printf("%d is \'%s\'\n", n, GetWord(buffer, n)) //Save having to type this for every test
int main(int argc, char *argv[]) {
char buffer[64];
TEST(0);
TEST(1);
TEST(5);
TEST(9);
TEST(10);
TEST(11);
TEST(12);
TEST(19);
TEST(20);
TEST(31);
TEST(42);
TEST(100);
TEST(101);
TEST(109);
TEST(110);
TEST(115);
TEST(120);
TEST(151);
TEST(546);
TEST(1000);
TEST(1001);
TEST(1024);
TEST(1564);
TEST(1000000);
TEST(1000000000);
TEST(1561981328);
TEST(-2147483647);
return 0;
}