Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C C# ASP.NET GimmeCode Homework , +
Hello,
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"
 
Thanks
Posted 22-Jan-13 10:47am
Edited 22-Jan-13 11:03am
v2
Comments
Marcus Kramer at 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 at 22-Jan-13 18:15pm
   
is this homework, i don`t do homeworks any more
Sergey Alexandrovich Kryukov at 22-Jan-13 18:31pm
   
"onefour"? Wow! Why?!
—SA
jibesh at 22-Jan-13 18:43pm
   
looks like the teacher visits CP at times so changed the question ;)
Rate this: bad
good
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.
  Permalink  
Comments
Sergey Alexandrovich Kryukov at 22-Jan-13 18:33pm
   
Wrong answer: it won't help to output "onefour" :-) My 5, of course.
—SA
jibesh at 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 at 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 at 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 at 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
Sergey Alexandrovich Kryukov at 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 :-).
—SA
Rate this: bad
good
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.
 
Cheers
Andi
 
PS: That's almost too much helped... Wink | ;-)
  Permalink  
v2

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

  Print Answers RSS
0 OriginalGriff 439
1 ChintanShukla 310
2 Gihan Liyanage 298
3 RyanDev 250
4 Richard Deeming 220
0 Sergey Alexandrovich Kryukov 8,906
1 OriginalGriff 7,771
2 CPallini 2,603
3 Richard MacCutchan 2,121
4 Abhinav S 1,928


Advertise | Privacy | Mobile
Web01 | 2.8.140827.1 | Last Updated 22 Jan 2013
Copyright © CodeProject, 1999-2014
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