#include "inttoeng.h" #include "string.h" const char *digit_array[] = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" }; const char *tens_array[] = { "", "", "twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninety" }; int intToEnglish(int i, char buf[]) { if(i < 0 || i > 9999) return 0; buf[0] = '\0'; char spacer[2]; // by using this array, we can make sure not to add // unnecessary whitespace by always prepending it to // each word, and turning it from the empty string to // the string containing a space after the first word spacer[0] = '\0'; spacer[1] = '\0'; int t = i/1000; if(t) { strcat(buf, digit_array[t]); strcat(buf, " thousand"); spacer[0] = ' '; } int h = (i%1000)/100; if(h) { strcat(buf, spacer); strcat(buf, digit_array[h]); strcat(buf, " hundred"); spacer[0] = ' '; } int to = (i%100); if(to == 0) { // no-op } else if(to < 20) { strcat(buf, spacer); strcat(buf, digit_array[to]); } else { int tens = to/10; // tens is at least 2 because to >= 20 strcat(buf, spacer); strcat(buf, tens_array[tens]); int o = to%10; if(o) { strcat(buf, "-"); strcat(buf, digit_array[o]); } } if(i == 0) strcat (buf, "zero"); return 1; }