Click here to Skip to main content
13,055,047 members (74,244 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I want to pass integer in a function and display string related to it.
my method is
string myNoString(int no)

so if i pass 2, it should display "two" and if i pass 14 it should display "onefour"

Posted 22-Jan-13 10:47am
Updated 22-Jan-13 11:03am
Marcus Kramer 22-Jan-13 17:05pm
Reread your course notes as the solution to this will likely be in there. This is a very basic assignment and needs very little logic to complete.
nika2008 22-Jan-13 18:15pm
is this homework, i don`t do homeworks any more
Sergey Alexandrovich Kryukov 22-Jan-13 18:31pm
"onefour"? Wow! Why?!
jibesh 22-Jan-13 18:43pm
looks like the teacher visits CP at times so changed the question ;)
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Check this nice article Converting numbers to the word equivalent. [^] this will help you to give a start.
Sergey Alexandrovich Kryukov 22-Jan-13 18:33pm
Wrong answer: it won't help to output "onefour" :-) My 5, of course.
jibesh 22-Jan-13 18:35pm
You are right but i was trying to help him to get a start :-) with that solution OP can atleast output till "Ten"..

Thanks for the Fiver!! ;)
Andreas Gieriet 22-Jan-13 18:35pm
Not quite right. The OP asks for simple number output, not for human friendly number reading...;-) At least I understand the output example like that.
See also my solution #2.
Sergey Alexandrovich Kryukov 22-Jan-13 18:41pm
See this, from OP:

so if i pass 2, it should display "two" and if i pass 14 it should display "onefour"

Of course, I would not call it "human friendly", but this is not "simple number output" :-)
I wouldn't probably answer at all, by the way.
Andreas Gieriet 22-Jan-13 18:45pm
I did and I don't feel bad about it ;-)
Maybe he becomes interested in the topic once he sees how to do it. I don't know how poor his text book is regarding how to attack such a problem in general.
Sergey Alexandrovich Kryukov 22-Jan-13 18:51pm
His text book? Well... I would say, it depends on a reader more.
We usually remember a proverb: "For a good dancer, even his legs is a hassle". There is a more offensive folk variant of this proverb, mentioning some other important body parts of a dancer :-).
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Do the exercise on a sheet of paper:
1) write down how you would do it "manually"
2) write your instructions for "manual" execution of the task in some controll flow form, e.g.
general idea: take the digits from the right to left and store their word equivalent in a string.
1) if the number is zero, the result is "zero"
2) otherwise
   2.1) initialize the result string to empty string
   2.2) take the reminder of the division of the number by 10
   2.3) convert that reminder to the word equivalent and prefix the result string by that new word
   2.4) take the division by 10 of the number as new number value
   2.5) repeat 2.1)...2.5) until the new number value is zero
   2.6) the result is the result string

... or alike.
Play it through on paper to check if the description above (called "pseudo-code") behaves as required.
Now you understand the problem well enough.

Next step is to translate that into C#.
string ToWordEquivalent(int number)
   if (number == 0) return "0";
   // gimmik, not described above ;-)
   if (number < 0) return "minus " + ToWordEquivalent(-number);
   // one of the possible technique, called lookup table
   string[] words = new string[] { "zero", "one", ... };
   string result = string.Empty;
   while(number > 0)
       result = ...[number % 10] + " " + result;
       number = ... / ...;
   return result.Trim();

Fill in the missing parts, call the function in some test cases.


PS: That's almost too much helped... ;-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 22 Jan 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100