Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

print 123-as-one hundred twenty three

 
Tomendra Singh
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.



im tryng to develop a code that can print any digit into words,eg-
/*
100-one hundred

1999-one thousand nine hundred ninty nine

5087-five thousand eighty seven
*/
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, cool. So what have you done so far?

Here's what I would do: first, write a program that works only for the numbers 1 to 10. Test it, make sure it works for all values. Then extend it to work for values up to nineteen, and test it again. Then add support for 20-29, and test again.

Now you do something a little different, I think. Adding support for 30-39 is going to be more a generalization than an addition. When it works, test some more.

Then add support up to 99, and test some more.

Then add support for "100", and test some more.

Now add support up to 199, and test some more...

By breaking this into small, manageable problems, you'll have a much easier time of it.
 
Tomendra Singh
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thnk you sir , i will try it.
 
Seamus Minogue
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think I would go about it quite like that.

Think about how YOU know how to say a number...

like 5280

you see:
5 in the 1000's so 5 thousand
2 in the 100's so 2 hundred
8 in the 10's this is trickier since unlike >100 each group of 10 has a name
0 in the 1's which is of course just the number.


So it seems to me you really need to start with three modules:

A module to convert 0-9 which will be used by 1's and anything higher than 10's.

A module to convert 10 - 90. Of course this is only 9 different cases (ten,twenty,thirty,etc)

A module which knows the names of each step up (hundred, thousand,million,etc).



So the over all design in my mind is you would work from left to right on a string number.



Take the leftmost digit (5) pass it to a converter to get 5 = five
Pass its position in the string (5280: 5 is in the 4 spot) to the converter which gives you thousand

Take the next digit (2) pass it to the converter to get 2 = two
Pass its position in the string (5280: 2 is in the 3 spot) to the converter which gives you hundred
Append this result to the above

Take the next digit (8) Since this is in the 2nd position (10's) its a special case. Pass it to the special converter to get eighty
Append this result to the above

Take the next digit (0) pass it to the converter to get 0 = null (return nothing)


And we are done having converted 5280 = five thousand two hundred eighty


So its a pretty straight forward iterative process... If you wanted you could do it recursively rather than iteratively but the mechanics wouldn't change much.

Anyway, thats how I see the problem. Good luck.
 
Tomendra Singh
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you mr.Seamus Minogue i will try it also.
 
Alan Moore
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Seamus Minogue:
A module to convert 10 - 90. Of course this is only 9 different cases (ten,twenty,thirty,etc)

I count 18 cases: "ten", "eleven", "twelve", ... "nineteen", "twenty", "thirty", etc.
 
Seamus Minogue
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ya this turned out to be a little more complicated than I had layed out but still an enjoyable exercise.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic