Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Hello,
I want to pass integer in a function and display string related to it.
my method is
C#
string myNoString(int no)

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

Thanks
Posted
Updated 22-Jan-13 11:03am
v2
Comments
fjdiewornncalwe 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?!
—SA
Jibesh 22-Jan-13 18:43pm    
looks like the teacher visits CP at times so changed the question ;)

Check this nice article Converting numbers to the word equivalent. [^] this will help you to give a start.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 22-Jan-13 18:33pm    
Wrong answer: it won't help to output "onefour" :-) My 5, of course.
—SA
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.
Cheers
Andi
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.
—SA
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.
Cheers
Andi
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#.
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.

Cheers
Andi

PS: That's almost too much helped... ;-)
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900