Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#ASP.NET
how to print string, if i input "1" then print "One", if i input "12" then print "One Two", if i input "123" then print "One Two Three"
Posted 15-Nov-12 20:32pm
Comments
digimanus at 16-Nov-12 2:54am
   
read the string character by chraacter and via switch case translate number to word
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

I won't give you the whole code, but:
string inp = "123";
foreach (char c in inp)
    {
    string word = "";
    switch (c)
        {
        case '0': word = "zero "; break;
        case '1': word = "one "; break;
        ...
        }
    Console.Write(word);
    }
  Permalink  
Comments
Pro Idiot at 16-Nov-12 3:21am
   
This is the whole code ;) even taken care of spacing as well ..
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

Here is one approach
Create an enum
enum Number
       {
           zero,
           one,
           two,
           three,
           four,
           five,
           six,
           seven,
           eight,
           nine,
       }
 
Read each character from the string and get its corresponding text from enum
string Text = "";
string value = "01234";
for (int i = 0; i < value.Length; i++)
{
    Text += (Number)(int.Parse(value.Substring(i, 1))) + " ";
}
Console.WriteLine(Text);
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Hi
Try this:
 
int inNum  = 0;
string inString = inNum.ToString();
string outText = "";
for (int i=0;i<instring.length;i++)>
{
string number=inString.Substring(i,1);
string oneWordOutput="";
switch(number)
{
case "1":
oneWordOutput = "One";
break;
case "2":
oneWordOutput = "Two";
break;
case "3":
oneWordOutput = "Three";
break;
case "4":
oneWordOutput = "Four";
break;
case "5":
oneWordOutput = "Five";
break;
case "6":
oneWordOutput = "Six";
break;
case "7":
oneWordOutput = "Seven";
break;
case "8":
oneWordOutput = "Eight";
break;
case "9":
oneWordOutput = "Nine";
break;
case "0":
oneWordOutput = "Zero";
break;
default:
break;
}
If(outText="")
{
outText = oneWordOutput;
}
else
{
outText = outText + " " + oneWordOutput;
}
}
 
Jacques
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

Hi
 
I think, this will help you.
 
int intVal = 12345;          
            Char[] charVal = intVal.ToString().ToCharArray();
            string result = string.Empty;
            for (int i = 0; i < charVal.Length; i++)
            {
                switch (charVal[i])
                { 
                    case'1':
                        result = result + " One";
                        break;
                    case '2':
                        result = result + " Two";
                        break;
                    case '3':
                        result = result + " Three";
                        break;
                    case '4':
                        result = result + " Four";
                        break;
                    case '5':
                        result = result + " Five";
                        break;
                }
            }
 
Regards
Dominic
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 7

Hi
Please refer the following link. This may help you
 
How to convert a numeric value or currency to English words using C#[^]
 
Regards
Dominic
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

use above code before that perform split operation.
  Permalink  
Comments
Nelek at 16-Nov-12 4:01am
   
Please don't post solutions to add information, to ask something or to comment another user. - To add information to your message, you can use the widget "Improve question" at the bottom of your text. - To answer a user, you can use the widget "Have a question or comment?" (as I am doing right now with you) or the widget "reply" in another comment.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

but i want if i input "123" then print "one hundred and twenty-three" Smile | :)
  Permalink  
Comments
digimanus at 16-Nov-12 3:29am
   
that is a change of question. You completely state something else as above in your question. Trolltime?
Nelek at 16-Nov-12 4:02am
   
Please don't post solutions to add information, to ask something or to comment another user. - To add information to your message, you can use the widget "Improve question" at the bottom of your text. - To answer a user, you can use the widget "Have a question or comment?" (as I am doing right now with you) or the widget "reply" in another comment.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 8

It's a very simple solution y you are treating it as very crucial thing just follow the instructions and do it.
  Permalink  

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 840
1 OriginalGriff 407
2 Abhinav S 303
3 Manas Bhardwaj 285
4 thatraja 210
0 Sergey Alexandrovich Kryukov 7,967
1 OriginalGriff 4,332
2 Peter Leow 3,699
3 Maciej Los 3,515
4 Er. Puneet Goel 3,107


Advertise | Privacy | Mobile
Web02 | 2.8.140415.2 | Last Updated 16 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid