how to change all chars in string with numbers in C#?

486 views Asked by At

i want to change chars with numbers, for example: a with 1, b with 2... z with 26. so the string "hello" will be something like this: 82491513621. the first question is: how to do this with easiest way, and the second: how to do this with SWITCH statement. i tried this, but after break; it stops. thanks.

3

There are 3 answers

2
Tudor On BEST ANSWER

You need a loop, whatever you do. A simple switch is not enough.

string input = "hello";
StringBuilder output = new StringBuilder();
foreach(char c in input)
{
   output.Append((c - 'a' + 1));
}

Console.WriteLine(output);

Some explanation: since all letters have a numeric ASCII code associated in alphabetic order, it means that from any char representing a letter we can subtract 'a' and add 1 to get its numeric rank in the alphabet.

If using the switch statement is an absolute requirement, you will need to write a separate case for each possible value of a character:

string input = "hello";
StringBuilder output = new StringBuilder();
foreach(char c in input)
{
    switch(c)
    {
        case 'a': output.Append("1"); break;
        case 'b': output.Append("2"); break;
        // etc.
        case 'z': output.Append("26"); break;
    }             
}

Console.WriteLine(output);
1
ken2k On

Following pieces of code are based on the ASCII representation of the letters.

  • A is represented by the value 65
  • B is represented by the value 66
  • ...etc.

From char array:

char[] letters = { 'A', 'B', 'C', 'D' };
int[] numbers = new int[4];

for (int i = 0; i < 4; i++)
{
    numbers[i] = Convert.ToByte(letters[i], CultureInfo.InvariantCulture) - 64;
}

Alternate way:

string myString = "HELLO";
StringBuilder stringAsNumbers = new StringBuilder();

for (int i = 0; i < myString.Length; i++)
{
    stringAsNumbers.Append(Convert.ToByte(myString[i], CultureInfo.InvariantCulture) - 64);
}

Take care of the case sensitivity. The code above works for upper case only, as 65 is the ASCII code for 'A' (not 'a').

Don't try the switch statement, it'll look horrible.

EDIT: If you really want the switch statement, here it is:

string myString = "AAB";
StringBuilder stringAsNumbers = new StringBuilder();

for (int i = 0; i < myString.Length; i++)
{
    switch (myString[i])
    {
        case 'A':
        case 'a':
            stringAsNumbers.Append("1");
            break;
        case 'B':
        case 'b':
            stringAsNumbers.Append("2");
            break;
        ...
    }
}

EDIT: To get the final string for the StringBuilder, use stringAsNumbers.ToString();

0
Baiyan Huang On

You could define a function like:

int transform(char ch)
{
}

This could either be implemented use a dictionary:

Dictionary<char, int> map;  
map['a'] = 1;
map['b'] = 2;
...
return map[ch];

or use switch as you mentioned:

switch(ch)
{
   case 'a': return 1;
   case 'b': return 2;
   ....
}

Now the function is ready, you could just iterate through your string and call transform against each characters.