What is the best way to make a mini-decoder?

1

Hello, I'm new to programming and would like to know the most efficient way to do a "mini-decoder". I took the idea of my teacher who gave a duty that we had to take the number and turn it into letters to form a word rsrs (maybe a little prezinho .. but ..)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
class Program
{
    static void Main(string[] args)
    {
        string str;
        string[] stra;

        while (true)
        {
            str = Console.ReadLine();             
            stra = str.Split(' ');




            for(int i = 0; i < stra.Length; i++)
            {
                switch (stra[i])
                {

                    case "1":
                        Console.Write("z");
                        break;
                    case "2":
                        Console.Write("x");
                        break;
                    case "3":
                        Console.Write("v");
                        break;
                    case "4":
                        Console.Write("u");
                        break;
                    case "5":
                        Console.Write("t");
                        break;
                    case "6":
                        Console.Write("s");
                        break;
                    case "7":
                        Console.Write("r");
                        break;
                    case "8":
                        Console.Write("q");
                        break;
                    case "9":
                        Console.Write("p");
                        break;
                    case "10":
                        Console.Write("o");
                        break;
                    case "11":
                        Console.Write("n");
                        break;
                    case "12":
                        Console.Write("m");
                        break;
                    case "13":
                        Console.Write("l");
                        break;
                    case "14":
                        Console.Write("j");
                        break;
                    case "15":
                        Console.Write("i");
                        break;
                    case "16":
                        Console.Write("h");
                        break;
                    case "17":
                        Console.Write("g");
                        break;
                    case "18":
                        Console.Write("f");
                        break;
                    case "19":
                        Console.Write("e");
                        break;
                    case "20":
                        Console.Write("d");
                        break;
                    case "21":
                        Console.Write("c");
                        break;
                    case "22":
                        Console.Write("b");
                        break;
                    case "23":
                        Console.Write("a");
                        break;
                }

            }
            Console.Write("\n");

        }





    }
}
}

Good .. you write the sequence of numbers (from 1 to 23) separated by space and it turns the numbers into letters, forming the word .. I wonder if this is the most efficient way to do this and if you has some "exercises" for me to train -q

PS: Yes, there is no k, y, w purposely at the request of the teacher .. rsrs

    
asked by anonymous 22.07.2014 / 23:13

1 answer

4

Without removing the letters k, y, and w (which were incorporated into the Portuguese alphabet in the new rule) (as soon as 'a' is going to be 26 and so on) you may think as follows.

Each character is already a number, and this number is given by the ASCII table. This is the lowercase part:

97    61    a
98    62    b
99    63    c
100   64    d
101   65    e
102   66    f
103   67    g
104   68    h
105   69    i
106   6A    j
107   6B    k
108   6C    l
109   6D    m
110   6E    n
111   6F    o
112   70    p
113   71    q
114   72    r
115   73    s
116   74    t
117   75    u
118   76    v
119   77    w
120   78    x
121   79    y
122   7A    z

The first number is the value in decimal and the second in hex. The letter comes next (I took the part in octal I know why: P)

Now, knowing that 'a' is worth 97, 'b' 98 and so on and your 'code', and easier to convert: just take your number and add something. If it is a 1, we want to add 121 , which is 122, the 'z' code.

Now we just need a generic rule to give a number, get the character given by it. Be this number x . The rule is as follows:

(27 - x) + 96

This will be the number of the new character. For example, for 'a', which is 26, we have:

(27 - 26) + 96 == 97

and for 'z', which is 1:

(27 - 1) + 96 == 122

And so on. After doing this account, you just need to print the character again. Soon

Console.Write((char)x);

will print the corresponding letter.

    
22.07.2014 / 23:37