#include #include #define WORDS_LEN 1000 #define INPUT_LEN 4 #define MAX_DIGITS 3 void getInput(char *input); void getDigits(char *input, int *hundreds, int *tens, int *ones); void convertToString(char *input, char *words); void convertHundreds(int hundreds, char *words); void convertTens(int tens, char *words); void convertTeens(int tens, int ones, char *words); void convertOnes(int tens, int ones, char *words); int main() { char input[INPUT_LEN]; char words[WORDS_LEN]; getInput(input); printf("Value entered was %s \n", input ); convertToString(input,words); printf("Conversion to words is \n%s \n", words ); return 0; } void getInput(char *input) { printf("Please input an integer value between 1 less than 1000\n"); fgets(input,INPUT_LEN,stdin); char *nl = strchr(input,'\n'); if (nl != 0) *nl = 0; return; } void getDigits(char *input, int *hundreds, int *tens, int *ones) { int digits[MAX_DIGITS] = { 0, 0, 0 }; int numDigits = strlen(input)-1; int curDigit = MAX_DIGITS-1; int i; for (i=numDigits; i>= 0; i--,curDigit--) { digits[curDigit] = input[i]-'0'; // printf("digit %d %d is %d \n", i, curDigit, input[i]-'0'); } *hundreds = digits[0]; *tens = digits[1]; *ones = digits[2]; return; } void convertToString(char *input, char *words) { int hundreds, tens, ones; getDigits(input, &hundreds, &tens, &ones); words[0] = 0; convertHundreds(hundreds,words); convertTens(tens,words); convertTeens(tens,ones,words); convertOnes(tens,ones,words); return; } void convertHundreds(int hundreds, char *words) { char *strings[10] = { "", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" }; if (hundreds < 1) return; strncat(words, strings[hundreds], WORDS_LEN); strncat(words," hundred ", WORDS_LEN); return; } void convertTens(int tens, char *words) { char *strings[10] = { "", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" }; if (tens <2) return; strncat(words,strings[tens],WORDS_LEN); strncat(words,"-",WORDS_LEN); return; } void convertTeens(int tens, int ones, char *words) { char *strings[10] = { "tens", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" }; if (tens != 1) return; strncat(words, strings[ones], WORDS_LEN); return; } void convertOnes(int tens, int ones, char *words) { char *strings[10] = { "", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" }; if (tens == 1) return; strncat(words, strings[ones], WORDS_LEN); return; }