// random symbol Random r = new Random(); int x = 0; int y = 1000; string[] symb = new string[] { "a", "b", "c", "d", "e", "f", "g", "x", "y", "z" }; int num = r.Next(x,y); string snum = Convert.ToString(num); Console.WriteLine(snum); string generate = ""; string[] gen = new string[10]; for (int i = 0; i < 10; i++) { if (snum.Contains(""+i)) { gen[i] = symb[i]; generate += gen[i]; } } Console.WriteLine(generate); Console.ReadLine(); 

A random number is generated, then it is decrypted into a character set. For example, 675 will be equal to "gxf". But there is one mistake, because of which I open this question, and that's what it is. If the numbers are repeated, he decrypts them as one letter, for example: 606 = "ez"; How to write a variable so that it writes the same letters, rather than deleting one of the 2 that were originally.

  • some kind of strange line coding that is independent of the line. Cycle on the line itself, and for each character from the line, search for the desired character - Grundy
  • If it's not a secret, why do you do such coding, what purpose are you pursuing? - sp7
  • judging by the number of letters - you just want to replace the numbers with letters, and the same number always changes to the same letter, am I right? Let us leave aside the question why, write off on the "want". - rdorn
  • @rdorn exactly like that - smily_prg
  • one
    @smily_prg ru.stackoverflow.com/q/416584/198316 I think here you will find suitable sources for yourself. Well, Skienna, Kormen, Knut (in order of increasing complexity and volume of the material). For a very, very beginning, you can search for a problem book of Shen or solve puzzles from the same Euler projecteuler.net

1 answer 1

Judging by the number of letters - you just want to replace the numbers with letters, and the same number always changes to the same letter.

First, correct the error and remove the excess

 while (num > 0) { generate = symb[num % 10] + generate; num /= 10; } 

This is enough to parse the number into numbers and put the letters into a string. I note that you do not need to turn a number into a string, the numbers from it are already easily extracted, though you will have to collect the string from the end.

There may be a small optimization, which in this case is not too critical, but with more complex problems with strings - is required. Change the type of the generate variable to StringBuilder :

 var generate = new StringBuilder(); 

Then adding a character to the beginning of the line will look like this:

 generate.Insert(0,symb[num % 10]); 

What does this give? String objects in .NET are immutable, which means that when concatenating strings, a new string is formed, and everything that is not needed waits for garbage collection and takes up memory. StringBuilder - represents a modifiable string, and with any manipulations with it new objects are not created, and therefore they do not clog the memory with garbage. This is especially important when you generate a long string in a loop. In the case of 3-4-digit numbers, this certainly will not give a noticeable gain.

Also replace

 string[] symb = new string[] { "a", "b", "c", "d", "e", "f", "g", "x", "y", "z" }; 

on

 string symb = "abcdefgxyz"; 

The rest of the code can be changed, the characters on the string can be accessed by index symb[i]

  • Thank you so much - smily_prg