Convert numbers to words

Discussion in 'C Programming' started by Abhishek Jha, Jan 5, 2005.

  1. Abhishek Jha

    Abhishek Jha Guest

    How can we write a shortest program in c to convert given number to
    words format. example input 5012 Output: five thousand tewlve.
    Abhishek Jha, Jan 5, 2005
    #1
    1. Advertising

  2. Abhishek Jha

    Jack Klein Guest

    On 4 Jan 2005 20:29:01 -0800, (Abhishek Jha)
    wrote in comp.lang.c:

    > How can we write a shortest program in c to convert given number to
    > words format. example input 5012 Output: five thousand tewlve.


    You can use any general purpose editor that can save files as plain
    text.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
    Jack Klein, Jan 5, 2005
    #2
    1. Advertising

  3. Abhishek Jha wrote:
    > How can we write a shortest program in c to convert given number to
    > words format. example input 5012 Output: five thousand tewlve.


    If I interpret you literally, the shortest possible C program on a
    platform with 32-bit integers would probably use the number to seek into
    a 100GB file and read the string out. I imagine this is rather
    unfeasible for a homework assignment.
    --
    Derrick Coetzee
    I grant this newsgroup posting into the public domain. I disclaim all
    express or implied warranty and all liability. I am not a professional.
    Derrick Coetzee, Jan 5, 2005
    #3
  4. Abhishek Jha

    Ben Pfaff Guest

    (Abhishek Jha) writes:

    > How can we write a shortest program in c to convert given number to
    > words format. example input 5012 Output: five thousand tewlve.


    I've posted this here several times before. I have a version for
    Japanese too if you want that.

    #include <stdio.h>
    #include <stdlib.h>

    char *ordinal (char buf[1024], int value);

    int
    main (int argc, char *argv[])
    {
    char buf[1024];

    if (argc != 2)
    {
    printf ("usage: ordinal <number>\n");
    return EXIT_FAILURE;
    }

    ordinal (buf, atoi (argv[1]));
    puts (buf);
    return EXIT_SUCCESS;
    }

    /* Works for positive and negative numbers up to 9 digits long,
    if you speak American English. */
    char *
    ordinal (char buf[1024], int value)
    {
    static const char *const powers[]
    = {"thousand", "million", "billion"};

    static const char *const ones[]
    = {"one", "two", "three", "four", "five",
    "six", "seven", "eight", "nine", "ten",
    "eleven", "twelve", "thirteen", "fourteen", "fifteen",
    "sixteen", "seventeen", "eighteen", "nineteen"};

    static const char *const tens[]
    = {"twenty", "thirty", "forty", "fifty",
    "sixty", "seventy", "eighty", "ninety"};

    char *cp = buf;

    if (value < 0)
    {
    cp += sprintf (cp, "negative ");
    value = -value;
    }
    else if (value == 0)
    {
    strcpy (buf, "zero");
    return buf;
    }

    {
    int part_stack[4];
    int *part_ptr = part_stack;

    for (; value; value /= 1000)
    *part_ptr++ = value % 1000;

    while (part_ptr > part_stack)
    {
    int p = *--part_ptr;

    if (p >= 100)
    {
    cp += sprintf (cp, "%s hundred ", ones[p / 100 - 1]);
    p %= 100;
    }

    if (p >= 20)
    {
    if (p % 10)
    cp += sprintf (cp, "%s-%s ", tens[p / 10 - 2], ones[p % 10 - 1]);
    else
    cp += sprintf (cp, "%s ", tens[p / 10 - 2]);
    }
    else if (p > 0)
    cp += sprintf (cp, "%s ", ones[p - 1]);

    if (p && part_ptr > part_stack)
    cp += sprintf (cp, "%s ", powers[part_ptr - part_stack - 1]);
    }
    }

    cp[-1] = 0;
    return buf;
    }

    /*
    Local variables:
    compile-command: "gcc -W -Wall -ansi -pedantic ordinal.c -o ordinal"
    End:
    */

    --
    "When in doubt, treat ``feature'' as a pejorative.
    (Think of a hundred-bladed Swiss army knife.)"
    --Kernighan and Plauger, _Software Tools_
    Ben Pfaff, Jan 5, 2005
    #4
  5. Ben Pfaff wrote:
    >
    > ordinal (buf, atoi (argv[1]));
    > ...
    > /* Works for positive and negative numbers up to 9 digits long,
    > if you speak American English. */
    > char *
    > ordinal (char buf[1024], int value)


    You assume an int can store a 9 digit integer. I think you should
    change the parameter to long, and (also) use strtol instead of atoi.
    --
    Peter
    Peter Nilsson, Jan 5, 2005
    #5
  6. Abhishek Jha

    Ben Pfaff Guest

    "Peter Nilsson" <> writes:

    > Ben Pfaff wrote:
    >>
    >> ordinal (buf, atoi (argv[1]));
    >> ...
    >> /* Works for positive and negative numbers up to 9 digits long,
    >> if you speak American English. */
    >> char *
    >> ordinal (char buf[1024], int value)

    >
    > You assume an int can store a 9 digit integer. I think you should
    > change the parameter to long, and (also) use strtol instead of atoi.


    If you supply a 9 digit integer to it, assuming that it is
    possible, it will convert it. That's all I was intending to
    state.
    --
    Ben Pfaff
    email:
    web: http://benpfaff.org
    Ben Pfaff, Jan 5, 2005
    #6
  7. Abhishek Jha

    Guest

    > I've posted this here several times before. I have a version for
    > Japanese too if you want that.

    :)..i like that...will that Japanese version compile on ANSI C
    compiler...-:)

    Cheers
    Waqas
    ------------------------------------------
    "You laught at me koz im different, i laugh at you koz you all are
    same" Kurt Cobaine
    , Jan 5, 2005
    #7
  8. Abhishek Jha

    Ben Pfaff Guest

    writes:

    >> I've posted this here several times before. I have a version for
    >> Japanese too if you want that.

    > :)..i like that...will that Japanese version compile on ANSI C
    > compiler...-:)


    It writes its output as romanized text, so it should compile and
    run everywhere (modulo any bugs).

    Here it is:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    char *ordinal (char buf[1024], unsigned value);

    int
    main (int argc, char *argv[])
    {
    char buf[1024];

    if (argc != 2)
    {
    printf ("usage: ordinal <number>\n");
    return EXIT_FAILURE;
    }

    ordinal (buf, atoi (argv[1]));
    puts (buf);
    return EXIT_SUCCESS;
    }

    char *
    ordinal (char buf[1024], unsigned value)
    {
    static const char *const powers[]
    = {"man", "oku"};

    static const char *const values[][9] =
    {
    {"sen", 0, "sanzen", 0, 0, 0, 0, "hassen", 0},
    {"hyaku", 0, "sanbyaku", 0, 0, "roppyaku", 0, "happyaku", 0},
    {"juu", 0, 0, 0, 0, 0, 0, 0, 0},
    {"ichi", "ni", "san", "yon", "go", "roku", "nana", "hachi", "kyuu"},
    };

    static const char *const *const ones = values[3];

    char *cp = buf;

    if (value == 0)
    {
    strcpy (buf, "rei");
    return buf;
    }

    {
    int part_stack[4];
    int *part_ptr = part_stack;

    for (; value; value /= 10000)
    *part_ptr++ = value % 10000;

    while (part_ptr > part_stack)
    {
    int index[4];
    int p, i;

    p = *--part_ptr;
    for (i = 3; i >= 0; i--)
    {
    index = p % 10;
    p /= 10;
    }

    for (i = 0; i < 4; i++)
    {
    int c = index;
    if (c != 0)
    {
    if (values[c - 1] == 0)
    cp += sprintf (cp, "%s%s ", ones[c - 1], values[0]);
    else
    cp += sprintf (cp, "%s ", values[c - 1]);
    }
    }

    if (*part_ptr && part_ptr > part_stack)
    cp += sprintf (cp, "%s ", powers[part_ptr - part_stack - 1]);
    }
    }

    cp[-1] = 0;
    return buf;
    }

    /*
    Local variables:
    compile-command: "gcc -W -Wall -ansi -pedantic ordinal-jp.c -o ordinal-jp"
    End:
    */

    --
    "I don't have C&V for that handy, but I've got Dan Pop."
    --E. Gibbons
    Ben Pfaff, Jan 5, 2005
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter Strøiman
    Replies:
    1
    Views:
    2,070
    Peter Strøiman
    Aug 23, 2005
  2. Richard Heathfield
    Replies:
    7
    Views:
    350
    Barry Schwarz
    Oct 5, 2003
  3. utab

    Words Words

    utab, Feb 16, 2006, in forum: C++
    Replies:
    6
    Views:
    415
    Daniel T.
    Feb 16, 2006
  4. Replies:
    5
    Views:
    573
    Keith Thompson
    Jul 15, 2007
  5. Jordon Bedwell

    Convert words to numbers and back?

    Jordon Bedwell, Nov 24, 2007, in forum: Ruby
    Replies:
    9
    Views:
    117
    Phrogz
    Nov 25, 2007
Loading...

Share This Page