hi all, I need to convert a string of numbers ie 125 into words ie one hundred and twenty five. So far all I have managed is to read the numbers in as a string into an array. I'm not the greatest at C :( any advice is much appreciated.

Printable View

- 04-14-2003Zimconverting numbers to words
hi all, I need to convert a string of numbers ie 125 into words ie one hundred and twenty five. So far all I have managed is to read the numbers in as a string into an array. I'm not the greatest at C :( any advice is much appreciated.

- 04-14-2003Magos
I'd use some kind of look-up table for that:

Code:`const char* DigitString[] = {`

"Zero",

"One",

"Two",

"Three",

"Four",

"Five",

"Six",

"Seven",

"Eight",

"Nine"};

const char* GetDigit(char DigitCharacter)

{

return DigitString[DigitCharacter - 48];

}

Remember to do special cases for 0, and for numbers 11 - 19, and for the deca numbers (10, 20, 30...). - 04-14-2003Dave_Sinkula
- 04-14-2003Zim
Thanks so much for your help guys :)

- 04-16-2003Zim
ok I've got it to work for 1 - 999 minus the teen numbers. Anyone got any tips for those? Thanks all.

- 04-16-2003Twiggy
I've always wondered this. Still learning C myself. What exactly does this do?

Code:`const char* GetDigit(char DigitCharacter)`

{

return DigitString[DigitCharacter - 48];

}

- 04-16-2003MagosQuote:

*Originally posted by Twiggy*

**I've always wondered this. Still learning C myself. What exactly does this do?**

Code:`const char* GetDigit(char DigitCharacter)`

{

return DigitString[DigitCharacter - 48];

}

- 04-16-2003Dave_SinkulaQuote:

ok I've got it to work for 1 - 999 minus the teen numbers. Anyone got any tips for those?

Quote:

I've always wondered this. Still learning C myself. What exactly does this do?Code:`const char* GetDigit(char DigitCharacter)`

{

return DigitString[DigitCharacter - 48];

}

- DigitString[DigitCharacter - 48] == DigitString[57 - 48] == DigitString[9]

Quote:

'0' == 48

- 04-16-2003quzahQuote:

*Originally posted by Twiggy*

**I've always wondered this. Still learning C myself. What exactly does this do?**

Code:`const char* GetDigit(char DigitCharacter)`

{

return DigitString[DigitCharacter - 48];

}

return foo[ bar - '0' ];

This attempts to avoid the issue described in the previous post. Numbers are, I believe, supposed to be represented sequentially from zero to nine in the character set, which is why this works. I don't recall if this is guarinteed or not.

Anyway, it's generally a good idea to avoid random constants such as that, so that people when they read your code actually know why there is some odd number in your code. Thus, you could do something like what I've illustrated, or you could macro it.

Code:`#define ZERO '0'`

return foo[ bar - ZERO ];

Quzah. - 04-16-2003Dave_SinkulaQuote:

Numbers are, I believe, supposed to be represented sequentially from zero to nine in the character set, which is why this works. I don't recall if this is guarinteed or not.

Quote:

Both the basic source and basic execution character sets shall have the following members:[...]

the 10 decimal*digits*

0 1 2 3 4 5 6 7 8 9

[...] In both the source and execution basic character sets, the value of each character after 0 in the above list of decimal digits shall be one greater than the value of the previous. [...]

- 04-17-2003Magos
Isn't the first 128 characters in ASCII a standard? So the order is always '0', '1', ... , '9'?

- 04-17-2003BrighteyesQuote:

Isn't the first 128 characters in ASCII a standard? So the order is always '0', '1', ... , '9'?

Code:`#include <stdio.h>`

int main(void)

{

char c = '5';

printf("%d\n", c - '0');

return 0;

}

- 05-04-2003Zim
#include <stdio.h>

char *units[11] = {"", "one", "two", "three", "four", "five", "six",

"seven", "eight", "nine", '\0'};

char *tens[11] = {"","eleven ","twelve ","thirteen ",

"fourteen ","fifteen ","sixteen ","seventeen ","eighteen ","nineteen ", '\0'};

char *twenties[11] = {"","ten", "twenty", "thirty", "forty", "fifty",

"sixty", "seventy", "eighty", "ninety", '\0'};

char *hundreds[11] = {"", "one hundred", "two hundred","three hundred","four hundred",

"five hundred","six hundred","seven hundred","eight hundred","nine hundred", '\0'};

char *thousands[11] = {"", "one thousand", "two thousand","three thousand","four thousand",

"five thousand","six thousand","seven thousand","eight thousand","nine thousand", '\0'};

char *Thousands[11] = {"", "teens!", "twenty", "thirty","forty","fifty",

"sixty","seventy","eighty","ninety", '\0'};

char *Hundreds[11] = {"", "one hundred", "two hundred","three hundred","four hundred",

"five hundred","six hundred","seven hundred","eight hundred","nine hundred", '\0'};

char *millions[11] = {"", "one million", "two million","three million","four million",

"five million","six million","seven million","eight million","nine million", '\0'};

int num, ten, unit, twentie, hundred, thousand, Thousand, Hundred, million;

char *letters[28] = {"", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l",

"m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", '\0'};

char letter;

int main()

{

printf("Please enter an integer\n\n");

scanf("%d", &num);

unit=num%10;

twentie=((num%100)-unit)/10;

hundred=((num%1000)-unit-(twentie*10))/100;

thousand=((num%10000)-(hundred*100)-(twentie*10)-unit)/1000;

Thousand=((num%100000)-(thousand*1000)-(hundred*100)-(twentie*10)-unit)/10000;

Hundred=((num%1000000)-(Thousand*10000)-(thousand*1000)-(hundred*100)-(twentie*10)-unit)/100000;

million=(num-((Hundred*100000)-(Thousand*10000)-(thousand*1000)-(hundred*100)-(twentie*10)-unit))/1000000;

if (num<10000000)

printf("\n\nmillions = %d, Hundreds = %d, Thousands = %d, thousands = %d, hundreds = %d, twenties = %d, units = %d\n\n", million, Hundred, Thousand, thousand, hundred, twentie, unit);

else printf("That number is too big\n\n");

if (num<10000000)

printf("\n\n%d in words is %s %s %s %s %s %s %s\n\n", num, millions [million], Hundreds [Hundred], Thousands [Thousand],

thousands [thousand], hundreds [hundred], twenties[twentie],units[unit]);

else 0;

return 0;

}

this is basically what I have so far. I think it needs a for loop to only do the function once and then repeat for infinite numbers. Still having trouble with the teens :( also would like to make it not work if letters are input. Any help greatly appreciated! I have tried!!! - 05-04-2003WaltP
Your char strings seem to be overkill. Four arrays should be enough I believe:

ones array for 0 to 9

teens array for 10 to 19

tens array for 10, 20, 30... 90

magnitude* array for 'thousand','million', 'billion', ... 'vigintillion'

and a hundred indicator

Parse the number and pass each set of 3 digits and its magnitude to a "translator()" routine to translate the 0-999 and add the magnitute designation.

Deciding to parse 1200 as 'twelve hundred' instead of 'oun thou, two hund' causes additional complexity.

* see http://www.math.pku.edu.cn/stu/ereso...ath/l/l096.htm - 05-06-2003Zim
Ok thanks. Can you explain the magnitude array more?