Click here to Skip to main content
15,888,802 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
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
Comments
Herman<T>.Instance 16-Nov-12 2:54am    
read the string character by chraacter and via switch case translate number to word

I won't give you the whole code, but:
C#
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);
    }
 
Share this answer
 
Comments
Pro Idiot 16-Nov-12 3:21am    
This is the whole code ;)

even taken care of spacing as well ..
Here is one approach
Create an enum
C#
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
C#
string Text = "";
string value = "01234";
for (int i = 0; i < value.Length; i++)
{
    Text += (Number)(int.Parse(value.Substring(i, 1))) + " ";
}
Console.WriteLine(Text);
 
Share this answer
 
Hi Try this:

C#
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
 
Share this answer
 
Hi
I think, this will help you.

C#
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
 
Share this answer
 
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
 
Share this answer
 
use above code before that perform split operation.
 
Share this answer
 
Comments
Nelek 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.
but i want if i input "123" then print "one hundred and twenty-three" :)
 
Share this answer
 
Comments
Herman<T>.Instance 16-Nov-12 3:29am    
that is a change of question. You completely state something else as above in your question. Trolltime?
Nelek 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.
It's a very simple solution y you are treating it as very crucial thing just follow the instructions and do it.
 
Share this answer
 

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