OK, how about this?

#include <stdio.h>

int main()
{
const char* singles[20]={"Zero","One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen"}; const char* tens[11]={"","Ten","Twenty","Thirty","Forty","Fifty","Sixty","Seventy","Eighty","Ninety","One Hundred"};

       int i;
       for (i=0; i <= 100; i++)
       {
               if (i < 20)
               {
                       printf("%d is %s\n",i,singles[i]);
               }
               else
               {
printf("%d is %s %s\n",i,tens[i/10],((i%10 == 0) ? "" : singles[i%10]));
               }
       }
}


Steve wrote:
Yeah thats nice but then you have a 100 unit array, seems to me it
could probably be done in less than 30 lines if I can work out how to
handle place value in C

On 5/2/06, Walter Holladay <[EMAIL PROTECTED]> wrote:
The easiest way would be to simply create an array with all the numbers
as words, and then just use the number as an index into the array.
Like so:

int main()
{
        const char*
nums[10]={"Zero","One","Two","Three","Four","Five","Six","Seven","Eight","Nine"};

        int i;
        for (i=0; i < 10; i++)
        {
                printf("%d is %s\n",i,nums[i]);
        }
}

Cheers,
Walter

Steve wrote:
> Hello all,
> This is prolly a little offtopic, but I'm wondering if anyone can
> recommend a good quick method of converting numbers to text.
> For instance if I wanted to replace all instances of 100 with the
> words One Hundred, is there something already written, a library
> somewhere?  This seems like something that should have been tackled
> ages ago and is probably a part of some entry level C++ courses, but
> the only way I can think of doing it would be one helaciously long
> switch statement.  Fortunately this would only need to cover the
> numbers 0 to 100.
> It does need to be done in C/C++ though.
>
> Any recommendations on a good lib for something like this, or an
> example snippet that doesn't result in a 100+ line switch statement?
>
> Thanks in advance.
> (BTW the numbers are already stored in a stringor actually a const char*)
>
> /*
> PLUG: http://plug.org, #utah on irc.freenode.net
> Unsubscribe: http://plug.org/mailman/options/plug
> Don't fear the penguin.
> */

/*
PLUG: http://plug.org, #utah on irc.freenode.net
Unsubscribe: http://plug.org/mailman/options/plug
Don't fear the penguin.
*/


/*
PLUG: http://plug.org, #utah on irc.freenode.net
Unsubscribe: http://plug.org/mailman/options/plug
Don't fear the penguin.
*/

/*
PLUG: http://plug.org, #utah on irc.freenode.net
Unsubscribe: http://plug.org/mailman/options/plug
Don't fear the penguin.
*/

Reply via email to