# Thread: Converting Numbers to Words

1. Let's start from scratch.

First, you need to be able to convert values from 0 thru 999 correctly. The hundred's is easy. Then the ten's, then the one's.

Second, if ten's = 1 you have a special case. Add that logic next

Third, add the logic that does the above for every 3 digits to get the thousand, million, billion... you need to count from the right and skip every 3 values until you get to the highest triple, the process as above following it with the magnitude (million, trillion, etc.) The back yo 3 values and do it again.

2. Ok, so I managed to do it! Converting numbers to words that is. Thanks everyone

I've run into another problem.

I have a number entered as words and stored in a string. I want to convert it back into numbers: Here's my psuedo

Code:
```String contains words.

Search string for highest order word

Get numbers that pre-cede "million" term if found.

Convert these numbers (in word form) to numerical. Search word list for number.

Check if string has ended.

If string hasn't ended. Search for "thousand "term.

If found, get numbers that precede it.

Convert these numbers to numerical. Search word list for number.

If string hasn't ended, Search for "hundred" term.

etc..```

The problem I am having is, when I find the "million" term for instance, how do I retrieve the 1 to 3 words that occur before it, (and hence pass them to my "word lists" and retrieve the int equivalent to add) ?

3. I know you already solved the problem, but hey, what's another example?

Code:
```char * printcheck(char buffer[], char number[])
{
char * hundreds_n_ones[] =
{
"",
"One ",
"Two ",
"Three ",
"Four ",
"Five ",
"Six ",
"Seven ",
"Eight ",
"Nine "
};

char * specials[] =
{
"Ten ",
"Eleven ",
"Twelve ",
"Thirteen ",
"Fourteen ",
"Fifteen ",
"Sixteen ",
"Seventeen ",
"Eighteen ",
"Nineteen "
};

char * tens[] =
{
"",
"",
"Twenty ",
"Thirty ",
"Forty ",
"Fifty ",
"Sixty ",
"Seventy ",
"Eighty ",
"Ninety "
};

char * levels [] =
{
"",
"Thousand ",
"Million ",
"Billion ",
"Trillion ",
"Quintillion ",
"Sextillion ",
"Septillion ",
"Octillion ",
"Nintillion ",
"Lots "
};

buffer[0] = 0;

int length = strlen(number);

if(length == 0) return buffer;

int digit, mode, level, place = length;

bool special = false;

int max_level_index = sizeof(levels)/sizeof(levels[0])-1;

for(int i = 0; i < length; ++i, --place)
{
digit = number[i]-'0';

mode = place%3;

if(mode == 1)
{
if(special)
{
special = !special;

strcat(buffer, specials[digit]);
}
else
strcat(buffer, hundreds_n_ones[digit]);

level = (place-1)/3;

if(level)
{
if(level > max_level_index)
level = max_level_index;

digit = level;

strcat(buffer, levels[digit]);
}
}
else if(mode == 2)
{
if(digit == 1)
special = true;
else
strcat(buffer, tens[digit]);
}
else if(mode == 0)
{
strcat(buffer, hundreds_n_ones[digit]);

strcat(buffer, "Hundred ");
}
}

return buffer;
}```

4. Originally posted by denizengt The problem I am having is, when I find the "million" term for instance, how do I retrieve the 1 to 3 words that occur before it, (and hence pass them to my "word lists" and retrieve the int equivalent to add) ?
I'll try this for the third time....

If you found MILLION, copy everything from the beginning of he string to MILLION into a temp array, and process that array.

Then skip over MILLION, remember this position 'p' and search for THOUSAND. Repeat above from 'p' to the THOUSAND.

5. Ok, sorry to resurrect this issue once more.

I've got numbers to words working. Now, what I have to do, is convert numbers entered into stdin, into their word equivalents! I have spent a few weeks trying to figure out how to do this.

I think the problem is with my approach. Can anybody tell me how they would go about approaching this problem. I just can't seem to get my head around going the other way

I need to "convert" numbers entered in literal form, say STDIN = "one hundred billion" and produce the numerical equivalent. I know you've tried to help me, but I still can't do it.

6. you could have a single int, say int sum, and give each word its respective value. Then go about with commands like for "Twenty" do int=+20, "million" does sum=+1 000 000 and so on.