converting numbers to words

This is a discussion on converting numbers to words within the C Programming forums, part of the General Programming Boards category; hi all, I need to convert a string of numbers ie 125 into words ie one hundred and twenty five. ...

  1. #1
    Zim
    Zim is offline
    Registered User
    Join Date
    Apr 2003
    Posts
    5

    converting 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.

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    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];
    }
    Then traverse the string, get it's digit, transform it, then add the postfix (hounded, thousand...).
    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.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    A search might turn up a relevant post or two.
    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.*

  4. #4
    Zim
    Zim is offline
    Registered User
    Join Date
    Apr 2003
    Posts
    5
    Thanks so much for your help guys

  5. #5
    Zim
    Zim is offline
    Registered User
    Join Date
    Apr 2003
    Posts
    5
    ok I've got it to work for 1 - 999 minus the teen numbers. Anyone got any tips for those? Thanks all.

  6. #6
    Registered User Twiggy's Avatar
    Join Date
    Oct 2001
    Posts
    43
    I've always wondered this. Still learning C myself. What exactly does this do?

    Code:
    const char* GetDigit(char DigitCharacter)
    {
       return DigitString[DigitCharacter - 48];
    }
    A breakdown of each line would be mondo helpful. Especially the -48

  7. #7
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    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];
    }
    A breakdown of each line would be mondo helpful. Especially the -48
    '0' == 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.

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    ok I've got it to work for 1 - 999 minus the teen numbers. Anyone got any tips for those?
    Follow the link I posted previously, and examine the code Prelude posted. Incorporate what you glean from it into your code.
    I've always wondered this. Still learning C myself. What exactly does this do?
    Code:
    const char* GetDigit(char DigitCharacter)
    {
       return DigitString[DigitCharacter - 48];
    }
    A breakdown of each line would be mondo helpful. Especially the -48
    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.
    • DigitString[DigitCharacter - 48] == DigitString[57 - 48] == DigitString[9]
    DigitString[9] is a pointer to the string literal "nine". This pointer is returned by GetDigit.
    '0' == 48
    In ASCII (not always).
    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.*

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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];
    }
    A breakdown of each line would be mondo helpful. Especially the -48
    As per the post above mine, if you're going to do this, you really should do:

    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 ];
    Or whatever...

    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    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.
    The following are excerpts from C99, 5.2.1p3 (they are the same in 2.2.1 of the C89 draft).
    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. [...]
    It's a "shall".
    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.*

  11. #11
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    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.

  12. #12
    Open to suggestions Brighteyes's Avatar
    Join Date
    Mar 2003
    Posts
    204
    Isn't the first 128 characters in ASCII a standard? So the order is always '0', '1', ... , '9'?
    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 like
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        char c = '5';
    
        printf("%d\n", c - '0');
    
        return 0;
    }
    always works.
    p.s. What the alphabet would look like without q and r.

  13. #13
    Zim
    Zim is offline
    Registered User
    Join Date
    Apr 2003
    Posts
    5

    Unhappy

    #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!!!

  14. #14
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    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

  15. #15
    Zim
    Zim is offline
    Registered User
    Join Date
    Apr 2003
    Posts
    5
    Ok thanks. Can you explain the magnitude array more?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 12-21-2007, 12:38 PM
  2. Converting Numbers to Words
    By denizengt in forum C Programming
    Replies: 20
    Last Post: 11-05-2003, 08:19 PM
  3. converting numbers into words
    By Kozam in forum C Programming
    Replies: 2
    Last Post: 09-30-2003, 07:49 AM
  4. C++ program help(converting numbers to english words)
    By bama1 in forum C++ Programming
    Replies: 4
    Last Post: 10-08-2002, 01:17 AM
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 05:38 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21