Programming Challenge Numbers to Text?

Walter Holladay walter at holladays.org
Tue May 2 06:24:04 MDT 2006


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.
> */


More information about the PLUG mailing list