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.
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.
I'd use some kind of look-up table for that:
Then traverse the string, get it's digit, transform it, then add the postfix (hounded, thousand...).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...).
MagosX.com
Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime.
Thanks so much for your help guys
ok I've got it to work for 1 - 999 minus the teen numbers. Anyone got any tips for those? Thanks all.
I've always wondered this. Still learning C myself. What exactly does this do?
A breakdown of each line would be mondo helpful. Especially the -48Code:const char* GetDigit(char DigitCharacter) { return DigitString[DigitCharacter - 48]; }
'0' == 48Originally posted by Twiggy
I've always wondered this. Still learning C myself. What exactly does this do?
A breakdown of each line would be mondo helpful. Especially the -48Code:const char* GetDigit(char DigitCharacter) { return DigitString[DigitCharacter - 48]; }
MagosX.com
Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime.
Follow the link I posted previously, and examine the code Prelude posted. Incorporate what you glean from it into your code.ok I've got it to work for 1 - 999 minus the teen numbers. Anyone got any tips for those?Let's say you are using an ASCII system and you pass a '9' to GetDigit. An ASCII '9' has a decimal value of 57.I've always wondered this. Still learning C myself. What exactly does this do?A breakdown of each line would be mondo helpful. Especially the -48Code:const char* GetDigit(char DigitCharacter) { return DigitString[DigitCharacter - 48]; }DigitString[9] is a pointer to the string literal "nine". This pointer is returned by GetDigit.
- DigitString[DigitCharacter - 48] == DigitString[57 - 48] == DigitString[9]
In ASCII (not always).'0' == 48
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
As per the post above mine, if you're going to do this, you really should do:Originally posted by Twiggy
I've always wondered this. Still learning C myself. What exactly does this do?
A breakdown of each line would be mondo helpful. Especially the -48Code: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.
Or whatever...Code:#define ZERO '0' return foo[ bar - ZERO ];
Quzah.
Hope is the first step on the road to disappointment.
The following are excerpts from C99, 5.2.1p3 (they are the same in 2.2.1 of the C89 draft).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.It's a "shall".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. [...]
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
Isn't the first 128 characters in ASCII a standard? So the order is always '0', '1', ... , '9'?
MagosX.com
Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime.
In ASCII, yea. But there're other character sets in use where everything isn't consecutive. Fortunately, every character set I know of has the digits 0-9 in order with no spacing. That's why stuff likeIsn't the first 128 characters in ASCII a standard? So the order is always '0', '1', ... , '9'?
always works.Code:#include <stdio.h> int main(void) { char c = '5'; printf("%d\n", c - '0'); return 0; }
p.s. What the alphabet would look like without q and r.
#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!!!
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
Ok thanks. Can you explain the magnitude array more?