Re: Converting Numbers to Words in English by recursion.
- From: rossum <rossum48@xxxxxxxxxxxx>
- Date: Sat, 10 Mar 2007 13:19:48 +0000
On 9 Mar 2007 20:03:09 -0800, jedale@xxxxxxxxx wrote:
I am trying to convert numbers to there corresponding words but itNumbers are split into groups of three digits: 123,456,908,436. Your
only works for numbers under 1,000 but need it to work for 1 billion.
Here is the code I have so far:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
/* Prototype */
void print_num(int num);
char *digits[] = {
"zero", "one", "two", "three", "four", "five",
"six", "seven", "eight", "nine"};
char *tens[] = {
"ten", "twenty", "thirty", "fourty", "fifty", "sixty",
"seventy", "eighty", "ninety"};
char *places[] = {"hundred", "thousand", "hundred", "million",
"hundred", "billion"};
int main(){
int num = 120394;
print_num(num);
printf("%d ", num); /* Print that number */
}
/* Print out the number sequence */
void print_num(int num){
if(num < 10){
printf("%s ", digits[num]);
}
else if(num < 100){
//print_num(num/10);
printf("%s ", tens[num/10]);
print_num(num%10 +1);
}
else if(num < 1000){
print_num(num/100);
printf("%s ", places[num/1000]);
print_num(num/10);
}else{
print_num(num/10);
print_num(num%10);
}
}
program needs to take account of that. What you have handles most
three digit combinations (000 to 999) in most situations. You need to
work on where to put the other words that describe the three digit
combinations, and to handle the exceptions to the standard wording of
some three digit groups.
123,000 = "one hundred and twenty three thousand" (drop the "and" if
you are American). That is 123 -> "one hundred and twenty three" plus
some extra verbiage for the 000 at the end. Now think about
123,000,000 and 123,000,000,000.
Extend to 123,456,000, 123,456,908 and 123,456,908,436.
Look for patterns and think about how to program them.
rossum
.
- References:
- Converting Numbers to Words in English by recursion.
- From: jedale
- Converting Numbers to Words in English by recursion.
- Prev by Date: reverse engineering....moronic!??!
- Next by Date: Re: how to test this piece of C code
- Previous by thread: Re: Converting Numbers to Words in English by recursion.
- Next by thread: how to test this piece of C code
- Index(es):
Relevant Pages
|