What’s the best way to make a mini-decoder?

Asked

Viewed 124 times

1

Hello I am new in programming and I would like to know the most efficient way to make a "mini-decoder". I got the idea from my teacher who gave a duty that we had to take the number and turn it into letters to form a rsrs word (maybe a little bit of a price.. 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 have some "exercises" for me to train -q

PS: Yes, there is no k,y,w on purpose at the teacher’s request.. rsrs

1 answer

4


Without removing the letters k, y and w (which have been incorporated into the Portuguese alphabet in the new rule) (soon 'a' will be valid 26 and so on).

Each character is already a number, and that number is given by the ASCII table. This is the part of the minuscule letters:

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 hexa. The letter comes next (I took out the part in octal I know it because :P)

Now, knowing that 'a' is worth 97, 'b' 98 and so on and its "code", and easier to convert: just take your number and add something. If he’s a one, we’d like to add 121, than 122, the code of z'.

Now we only need a Generica rule to given a number, to obtain the character given by it. Be that number x. The rule and the following:

(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 worth 1:

(27 - 1) + 96 == 122

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

Console.Write((char)x);

will print the corresponding letter.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.