Switch case statement - c#

I am trying to create an application in C# that converts numbers in a text box to roman numerals in a label control and need to use a case statement. However one of my variable Roman gets the error message: Use of unassigned local variable 'Roman'.
Here is my code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Roman_Numeral_Converter
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnCalc_Click(object sender, EventArgs e)
{
int Number=int.Parse(txtNum.Text); // To hold Number
string Roman; // To hold Roman Numeral
if (Number>=1 && Number <=10)
{
switch (Roman)
{
case "Number==1":
lblRoman.Text = "I";
break;
case "Number==2":
lblRoman.Text = "II";
break;
case "Number==3":
lblRoman.Text = "III";
break;
case "Number==4":
lblRoman.Text = "IV";
break;
case "Number==5":
lblRoman.Text = "V";
break;
case "Number==6":
lblRoman.Text = "VI";
break;
case "Number==7":
lblRoman.Text = "VII";
break;
case "Number==8":
lblRoman.Text = "VIII";
break;
case "Number==9":
lblRoman.Text = "IX";
break;
case "Number==10":
lblRoman.Text = "X";
break;
}
}
else
{
MessageBox.Show("Error: Invalid Input");
}
}
private void btnExit_Click(object sender, EventArgs e)
{
// Close the form.
this.Close();
}
private void btnClear_Click(object sender, EventArgs e)
{
txtNum.Text = "";
lblRoman.Text = "";
}
}
}

Your structure is a little off.
private void btnCalc_Click(object sender, EventArgs e)
{
var Number = int.Parse(txtNum.Text); // To hold Number
switch (Number)
{
case 1:
lblRoman.Text = "I";
break;
case 2:
lblRoman.Text = "II";
break;
case 3:
lblRoman.Text = "III";
break;
case 4:
lblRoman.Text = "IV";
break;
case 5:
lblRoman.Text = "V";
break;
case 6:
lblRoman.Text = "VI";
break;
case 7:
lblRoman.Text = "VII";
break;
case 8:
lblRoman.Text = "VIII";
break;
case 9:
lblRoman.Text = "IX";
break;
case 10:
lblRoman.Text = "X";
break;
default:
MessageBox.Show("Error: Invalid Input");
break;
}
}
You're using the lblRoman to hold your result, thus your Roman variable is unnecessary. Additionally, since you're interrogating every possible valid number in your switch, you can just use the default to replace your if/else structure.
I'm assuming you're doing this as an academic exercise. That being said, I would be remiss not to point to you Mosè Bottacini's solution to this problem.

This is because Roman variable is really unassigned. You should assign it before you enter the statement

try this,
when your number value like 1 so roman number is I.
private void btnCalc_Click(object sender, EventArgs e)
{
int Number = int.Parse(txtNum.Text); // To hold Number
string Roman; // To hold Roman Numeral
if (Number >= 1 && Number <= 10)
{
switch (Number)
{
case 1:
lblRoman.Text = "I";
break;
case 2:
lblRoman.Text = "II";
break;
case 3:
lblRoman.Text = "III";
break;
case 4:
lblRoman.Text = "IV";
break;
case 5:
lblRoman.Text = "V";
break;
case 6:
lblRoman.Text = "VI";
break;
case 7:
lblRoman.Text = "VII";
break;
case 8:
lblRoman.Text = "VIII";
break;
case 9:
lblRoman.Text = "IX";
break;
case 10:
lblRoman.Text = "X";
break;
}
}
else
{
MessageBox.Show("Error: Invalid Input");
}
}

Instead of switch, You can do other way using Linq which is even better.
int Number=int.Parse(txtNum.Text);
var romanList = new List<string> {"I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X"};
if (Number >= 1 && Number <= 10)
lblRoman.Text = romanList.Select((r, i) => new { Roman = r, Index = i+1}).FirstOrDefault(x=> x.Index == Number).Roman;

you can replace your switch statement this way. and of course you need to assign a variable before using it.
public string GetNum(string val)
{
string res = ""; // Assign it an empty string.
var numToRom = new Dictionary<string, string>
{
{"1","I"},
{"2","II"}
//so on
};
numToRom.TryGetValue(val, out res);
return res;
}

Related

c# how to refer to current button

I am trying to make a minesweeper game using Windows Application. I would like to use a switch inside a method.
The problem:
public void switcher()
{
switch (x)
{
case 0:
A1.BackgroundImage = Image.FromFile("empty.jpg"); // look at A1
break;
case 1:
A1.BackgroundImage = Image.FromFile("1.jpg");
break;
case 2:
A1.BackgroundImage = Image.FromFile("2.jpg");
break;
case 3:
A1.BackgroundImage = Image.FromFile("3.jpg");
break;
case 4:
A1.BackgroundImage = Image.FromFile("4.jpg");
break;
case 5:
A1.BackgroundImage = Image.FromFile("5.jpg");
break;
case 6:
A1.BackgroundImage = Image.FromFile("6.jpg");
break;
case 7:
A1.BackgroundImage = Image.FromFile("7.jpg");
break;
case 8:
A1.BackgroundImage = Image.FromFile("8.jpg");
break;
}
}
As you can see, each of them says "A1. ....." A1 is my first button's name, but there are many other buttons as well. Is there a way I can refer to the button's properties, such as background image, without having to use its name? It would make programming so much easier.
Here's a simplified part of the button, if it helps:
private void A1_Click(object sender, EventArgs e) // < - I want to refer to this without using A1 name.
{
x = bombcount[0, 0];
switcher();
}
edit: One of the answers worked. I don't know much about these things, but I'll try to learn more about them! Thank you!
You need to refactor this code altogether. Don't rely on private members such as x like that, use method parameters:
private void SetButtonImage(Button button, int number)
{
string imagePath;
if (number == 0)
{
imagePath = "empty.jpg";
}
else
{
imagePath = number + ".jpg";
}
button.BackgroundImage = Image.FromFile(imagePath);
}
Then call it like this:
private void Button_Click(object sender, EventArgs e)
{
var button = sender as Button;
int number = bombcount[0, 0];
SetButtonImage(button, number);
}
Now you can hook up all button click events to that single event handler, and you can remove the x member, and you can remove the switch altogether.
Get a reference in switcher to the button being clicked like this:
private void A1_Click(object sender, EventArgs e)
{
Button myB = (Button) sender;
x = bombcount[0,0];
switcher(myB);
}
private void switcher(Button button)
{
switch (x)
{
case 0:
button.BackgroundImage = Image.FromFile("empty.jpg"); // look at A1
break;
case 1:
button.BackgroundImage = Image.FromFile("1.jpg");
break;
case 2:
button.BackgroundImage = Image.FromFile("2.jpg");
break;
case 3:
button.BackgroundImage = Image.FromFile("3.jpg");
break;
case 4:
button.BackgroundImage = Image.FromFile("4.jpg");
break;
case 5:
button.BackgroundImage = Image.FromFile("5.jpg");
break;
case 6:
button.BackgroundImage = Image.FromFile("6.jpg");
break;
case 7:
button.BackgroundImage = Image.FromFile("7.jpg");
break;
case 8:
button.BackgroundImage = Image.FromFile("8.jpg");
break;
}
}

Passing a variable from a method to a button click event

I want to be able to pass upperEncodedMsg into the text property of msgLabel at the bottom of my code.
namespace ProgrammingAssignmentDecoder
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void Encode()
{
string message = Convert.ToString(messageTxt.Text);
char[] encodeArray = message.ToCharArray();
for (int i = 0; i < encodeArray.Length; i++)
{
char letter = (encodeArray[i]);
switch (letter)
{
case 'a':
case 'A':
encodeArray[i] = 't';
break;
case 'b':
case 'B':
encodeArray[i] = 'u';
break;
case 'c':
case 'C':
encodeArray[i] = 'v';
break;
case 'd':
case 'D':
encodeArray[i] = 'w';
break;
case 'e':
case 'E':
encodeArray[i] = 'x';
break;
case 'f':
case 'F':
encodeArray[i] = 'y';
break;
case 'g':
case 'G':
encodeArray[i] = 'z';
break;
case 'h':
case 'H':
encodeArray[i] = 'a';
break;
case 'i':
case 'I':
encodeArray[i] = 'b';
break;
case 'j':
case 'J':
encodeArray[i] = 'c';
break;
case 'k':
case 'K':
encodeArray[i] = 'd';
break;
case 'l':
case 'L':
encodeArray[i] = 'e';
break;
case 'm':
case 'M':
encodeArray[i] = 'f';
break;
case 'n':
case 'N':
encodeArray[i] = 'g';
break;
case 'o':
case 'O':
encodeArray[i] = 'h';
break;
case 'p':
case 'P':
encodeArray[i] = 'i';
break;
case 'q':
case 'Q':
encodeArray[i] = 'j';
break;
case 'r':
case 'R':
encodeArray[i] = 'k';
break;
case 's':
case 'S':
encodeArray[i] = 'l';
break;
case 't':
case 'T':
encodeArray[i] = 'm';
break;
case 'u':
case 'U':
encodeArray[i] = 'n';
break;
case 'v':
case 'V':
encodeArray[i] = 'o';
break;
case 'w':
case 'W':
encodeArray[i] = 'p';
break;
case 'x':
case 'X':
encodeArray[i] = 'q';
break;
case 'y':
case 'Y':
encodeArray[i] = 'r';
break;
case 'z':
case 'Z':
encodeArray[i] = 's';
break;
}
}
}
static string upperEncoded(char[] encodeArray, string upperEncodedMsg)
{
string encodedMsg = new string(encodeArray);
upperEncodedMsg = encodedMsg.ToUpper();
return upperEncodedMsg;
}
private void clearBtn_Click(object sender, EventArgs e)
{
messageTxt.Text = string.Empty;
msgLabel.Text = string.Empty;
processedMessageLabel.Text = "Processed Message: ";
}
private void encodeBtn_Click(object sender, EventArgs e)
{
Encode();
if (messageTxt.TextLength == 0)
{
MessageBox.Show("There is no message to Encode");
}
else
{
processedMessageLabel.Text = "Encoded Message: ";
msgLabel.Visible = true;
}
msgLabel.Text = upperEncodedMsg;
}
}
}
I tried to keep your structure in tact, there are a few things that could be done in less characters and in less complicated ways, but as you are learning the language practice is the only thing that can improve your skills :) So well done.
namespace ProgrammingAssignmentDecoder
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private string upperEncodedMsg = null;
public void Encode()
{
string message = Convert.ToString(messageTxt.Text);
char[] encodeArray = message.ToCharArray();
for (int i = 0; i < encodeArray.Length; i++)
{
char letter = (encodeArray[i]);
switch (letter)
{
case 'a':
case 'A':
encodeArray[i] = 't';
break;
case 'b':
case 'B':
encodeArray[i] = 'u';
break;
case 'c':
case 'C':
encodeArray[i] = 'v';
break;
case 'd':
case 'D':
encodeArray[i] = 'w';
break;
case 'e':
case 'E':
encodeArray[i] = 'x';
break;
case 'f':
case 'F':
encodeArray[i] = 'y';
break;
case 'g':
case 'G':
encodeArray[i] = 'z';
break;
case 'h':
case 'H':
encodeArray[i] = 'a';
break;
case 'i':
case 'I':
encodeArray[i] = 'b';
break;
case 'j':
case 'J':
encodeArray[i] = 'c';
break;
case 'k':
case 'K':
encodeArray[i] = 'd';
break;
case 'l':
case 'L':
encodeArray[i] = 'e';
break;
case 'm':
case 'M':
encodeArray[i] = 'f';
break;
case 'n':
case 'N':
encodeArray[i] = 'g';
break;
case 'o':
case 'O':
encodeArray[i] = 'h';
break;
case 'p':
case 'P':
encodeArray[i] = 'i';
break;
case 'q':
case 'Q':
encodeArray[i] = 'j';
break;
case 'r':
case 'R':
encodeArray[i] = 'k';
break;
case 's':
case 'S':
encodeArray[i] = 'l';
break;
case 't':
case 'T':
encodeArray[i] = 'm';
break;
case 'u':
case 'U':
encodeArray[i] = 'n';
break;
case 'v':
case 'V':
encodeArray[i] = 'o';
break;
case 'w':
case 'W':
encodeArray[i] = 'p';
break;
case 'x':
case 'X':
encodeArray[i] = 'q';
break;
case 'y':
case 'Y':
encodeArray[i] = 'r';
break;
case 'z':
case 'Z':
encodeArray[i] = 's';
break;
}
}
foreach (char eachChar in encodeArray) {
upperEncodedMsg += eachChar;
}
}
public void upperEncoded()
{
if (upperEncodedMsg != null)
{ upperEncodedMsg = upperEncodedMsg.ToUpper(); }
}
private void clearBtn_Click(object sender, EventArgs e)
{
messageTxt.Text = string.Empty;
msgLabel.Text = string.Empty;
processedMessageLabel.Text = "Processed Message: ";
}
private void encodeBtn_Click(object sender, EventArgs e)
{
if (messageTxt.TextLength == 0)
{
MessageBox.Show("There is no message to Encode");
}
else
{
Encode();
upperEncoded();
processedMessageLabel.Text = "Encoded Message: ";
msgLabel.Visible = true;
}
msgLabel.Text = upperEncodedMsg;
}
}
}
I'd go down the route of creating a mapping using a Dictionary<char, char>(see here for details on Dictionary) and simply lookup the key value in the dictionary for each char in encodeArray. It may also be better to use a StringBuilder rather than just a string to store your encoded message.
Create Mapping
//Add Dictionary at the top of the class along with upperEncodedMsg string.
private Dictionary<char, char> charMapping;
private StringBuilder upperEncodedMsg;
//Create mappings. Use uppercase values here and you won't need to use your 'upperEncoded' method. This bit can be done in your Form1 constructor.
public Form1()
{
InitializeComponent();
charMapping = new Dictionary<char, char>();
charMapping.Add('A', 'T');
charMapping.Add('B', 'U');
//more mappings...
}
You can then remove the big switch statement you have and replace it with the following:
public void Encode()
{
upperEncodedMsg = new StringBuilder();
string message = Convert.ToString(messageTxt.Text);
char[] encodeArray = message.ToUpper().ToCharArray();
for(int i = 0; i < encodeArray.Length; i++)
{
//Use the mappings created earlier to get the associated char.
char outputLetter;
charMapping.TryGetValue(encodeArray[i], out outputLetter);
//Append letter to your upperEncodedMsg StringBuilder.
upperEncodedMsg.Append(outputLetter);
}
}
In your button click, at the bottom you can then add:
private void encodeBtn_Click(object sender, EventArgs e)
{
//your code...
msgLabel.Text = upperEncodedMsg.ToString();
}
The main benefit of doing it this way is that your mappings can be made available in other parts of your code. It also makes it easier to maintain and your mappings are loaded as soon as the form is, rather than waiting to call your encode method. A lookup of a Dictionary is also likely to be quicker than a large switch statement.

Convert decimals 1-10 to Roman Numeral; Vice versa via created class and methods

New project!
Write a Roman Numeral class. Given an integer value between 1 and 10, the Roman Numeral class should be able to convert this decimal value to a Roman Numeral. The class should also contain a method that would convert a Roman Numeral to its decimal equivalent. Write a second class (console or form) to test your Roman Numeral class. The second class should allow the user to input test values of each type.
So, what I have in my class is this:
namespace RomanNumber
{
class RomanNumeral
{
private int numToRoman;
private string romNumber;
private string romanToNum;
public RomanNumeral()
{
}
public RomanNumeral(int numNumber)
{
numToRoman = numNumber;
}
public int NumToRoman
{
get
{
return numToRoman;
}
set
{
numToRoman = value;
}
}
public string RomanToNum
{
get
{
return romanToNum;
}
set
{
romanToNum = value;
}
}
public string ReturnRomNum()
{
string romanNum;
switch (romNumber)
{
case '1':
romanNum = "I";
break;
case '2':
romanNum = "II";
break;
case '3':
romanNum = 'III';
break;
case '4':
romanNum = 'IV';
break;
case '5':
romanNum = 'V';
case '6':
romanNum = 'VI';
break;
case '7':
romanNum = 'VII';
break;
case '8':
romanNum = 'VIII';
break;
case '9':
romanNum = 'IX';
break;
case '10':
romanNum = 'X';
break;
default:
romanNum = "Error";
break;
}
return romanNum;
}
public int ReturnNomNum()
{
int numberNum;
switch (romanToNum)
case 'I':
romanToNum = "1";
break;
case 'II':
romanToNum = "2";
break;
case 'III':
romanToNum = '3';
break;
case 'IV':
romanToNum = '4';
break;
case 'V':
romanToNum = '5';
case 'VI':
romanToNum = '6';
break;
case 'VII':
romanToNum = '7';
break;
case 'VIII':
romanToNum = '8';
break;
case 'IX':
romanToNum = '9';
break;
case 'X':
romanToNum = '10';
break;
default:
romanToNum = "Error";
break;
}
return romanToNum;
}
}
}
I've seen other example of the RomanNumeral class all over, but they all look WAY more complicated than anything we've learned in class. Just wonder if I can take what I have and make it work once I create a test class. Please helP!! Thanks!
This smells like homework. I'll lend a hand and get you half way there with an int to roman method.
private string ToRoman(int input)
{
var lookup = new Dictionary<string, int> { {"M", 1000}, {"CM", 900}, {"D", 500}, {"CD", 400}, {"C", 100},
{"XC", 90}, {"L", 50}, {"XL", 40}, {"X", 10}, {"IX", 9}, {"V", 5}, {"IV", 4}, {"I", 1} };
var output = string.Empty;
var remainder = input;
foreach (var digit in lookup.Keys)
{
while (remainder >= lookup[digit])
{
output += digit;
remainder -= lookup[digit];
}
}
return output;
}

How to display a variable mantissa length

I have a label on a form that displays a float (_DataFloat) with a variable (_Digits) that sets the number of digits to show to the right of the decimal point. Assuming that _Digits can be any value from 0 through 6, is there a better way of formatting the text other than using a switch statement as below?
switch (_Digits) {
case 0:
label1.Text = _DataFloat.ToString("0");
break;
case 1:
label1.Text = _DataFloat.ToString("0.0");
break;
case 2:
label1.Text = _DataFloat.ToString("0.00");
break;
case 3:
label1.Text = _DataFloat.ToString("0.000");
break;
case 4:
label1.Text = _DataFloat.ToString("0.0000");
break;
case 5:
label1.Text = _DataFloat.ToString("0.00000");
break;
case 6:
label1.Text = _DataFloat.ToString("0.000000");
break;
default:
label1.Text = _DataFloat.ToString("0.00");
break;
}
How about:
var format = String.Format("0.{0}", new string('0', _Digits));
label1.Text = _DataFloat.ToString(format);

The case, breaks, and loops with c#

My application works if I type in 1 character, such as A. It will give me 10 2's,
but I want it to work on all 10 digits I type in. What am I doing wrong?
I want it so I can type in 1800HELLO2 and it will give me all digits.
class Program
{
static void Main(string[] args)
{
int x = 0;
string userInput;
Console.WriteLine("Please enter the 10 digit telephone number. ");
userInput = Console.ReadLine();
while (x < 10)
{
switch (userInput)
{
case "1":
userInput = "1";
x++;
break;
case "A":
case "B":
case "C":
case "2":
userInput = "2";
x++;
break;
case "D":
case "E":
case "F":
case "3":
userInput = "3";
x++;
break;
case "G":
case "H":
case "I":
case "4":
userInput = "4";
x++;
break;
case "J":
case "K":
case "L":
case "5":
userInput = "5";
x++;
break;
case "M":
case "N":
case "O":
case "6":
userInput = "6";
x++;
break;
case "P":
case "Q":
case "R":
case "7":
userInput = "7";
x++;
break;
case "S":
case "T":
case "U":
case "8":
userInput = "8";
x++;
break;
case "V":
case "W":
case "X":
case "Y":
case "Z":
userInput = "9";
x++;
break;
case "0":
userInput = "0";
break;
}
Console.WriteLine(userInput);
}
}
}
}
Your user input can be any number of characters (the ReadLine() function will read N characters until you press ENTER) but your switch statement doesn't check individual characters of your input - it only checks a number of 1-character strings.
This means that even if you type 1-800-333-1111, you'll never check it because it's not a 1-character string like the various cases you have in the switch.
You need to iterate through each character one-by-one in the input string and check the individual characters. For example:
if ( userinput != null )
{
userinput = userinput.ToUpper ();
for ( int i = 0; i < userinput.Length; i++ )
{
switch ( userinput[i] )
{
case '1':
case 'A':
...
break;
...
default:
// Handle invalid characters here
break;
}
}
}
Notice that the various case values are single characters (using the '), not 1-character strings.
Do note, that it's not a very good idea to hardcode the length of the phone number as a number in the code. Different users may enter the phone numbers differently - some may use spaces or dashes as separators, some may only enter the digit. In these cases the length of the input string will be different. Some users may even accidentally enter multiple spaces or put in ( and ) for the area code.
You should validate the input before you even check or you shouldn't rely on the number of input digits as you iterate through the input.
Your approach isn't really correct. You're switching on userInput every time, when really you want to check each character in the string. Study the code below:
using System;
class Program {
static void Main(string[] args) {
Console.WriteLine("Please enter the 10 digit telephone number. ");
string userInput = Console.ReadLine();
// Maybe do some validation here, check the length etc
Char output;
foreach (Char c in userInput) {
switch (c) {
case 'A':
case 'B':
case 'C':
output = '2';
break;
case 'D':
case 'E':
case 'F':
output = '3';
break;
case 'G':
case 'H':
case 'I':
output = '4';
break;
case 'J':
case 'K':
case 'L':
output = '5';
break;
case 'M':
case 'N':
case 'O':
output = '6';
break;
case 'P':
case 'Q':
case 'R':
output = '7';
break;
case 'S':
case 'T':
case 'U':
output = '8';
break;
case 'V':
case 'W':
case 'X':
case 'Y':
case 'Z':
output = '9';
break;
default:
output = c;
break;
}
Console.Write(output);
}
Console.WriteLine();
Console.ReadLine();
}
}
We use a foreach loop so we aren't hard coding the length, which gives flexibility.
Try this instead:
class Program
{
static void Main(string[] args)
{
int x = 0;
string userInput;
Console.WriteLine("Please enter the 10 digit telephone number. ");
userInput = Console.ReadLine();
// Did the user type in more than 10 characters?
if(userInput.Length > 10)
{
// Get the first ten letters, no matter how many letters the user entered
userInput = userInput.Substring(0, 10);
}
// Force values to upper case for comparison
userInput = userInput.ToUpper();
string systemOutput = String.Empty;
foreach(var c in userInput)
{
switch (c)
{
case "1":
systemOutput += "1";
break;
case "A":
case "B":
case "C":
case "2":
systemOutput += "2";
break;
case "D":
case "E":
case "F":
case "3":
systemOutput += "3";
break;
case "G":
case "H":
case "I":
case "4":
systemOutput += "4";
break;
case "J":
case "K":
case "L":
case "5":
systemOutput += "5";
break;
case "M":
case "N":
case "O":
case "6":
systemOutput += "6";
break;
case "P":
case "Q":
case "R":
case "7":
systemOutput += "7";
break;
case "S":
case "T":
case "U":
case "8":
systemOutput += "8";
break;
case "V":
case "W":
case "X":
case "Y":
case "Z":
systemOutput += "9";
break;
case "0":
systemOutput += "0";
break;
}
}
Console.WriteLine(systemOutput);
}
}
You can take benefit of Dictionary for this purpose:
static void Main(string[] args)
{
int x = 0;
string userInput;
Console.WriteLine("Please enter the 10 digit telephone number. ");
userInput = Console.ReadLine();
Dictionary<string,string> dict = new Dictionary<string,string>();
dict.Add("1","1");
dict.Add("ABC2","2");
dict.Add("DEF3","3");
dict.Add("GHI4","4");
dict.Add("JKL5","5");
dict.Add("MNO6","6");
dict.Add("PQR7","7");
dict.Add("STU8","8");
dict.Add("VWXYZ9","9");
dict.Add("0","0");
userInput = string.Join("",userInput.Select(c=>dict.First(k=>k.Key.Contains(c)).Value).ToArray());
Console.WriteLine(userInput);
}
or even more concise:
static void Main(string[] args)
{
int x = 0;
string userInput;
Console.WriteLine("Please enter the 10 digit telephone number. ");
userInput = Console.ReadLine();
string[] s = "0,1,ABC2,DEF3,GHI4,JKL5,MNOP6,PQR7,STU8,VWXYZ9".Split(',');
userInput = string.Join("",userInput.Select(c=>s.Select((x,i)=>new{x,i})
.First(k=>k.x.Contains(c)).i).ToArray());
Console.WriteLine(userInput);
}

Categories

Resources