I write the application - the game "Guess the word" on windowsforms.

The word word is randomly selected from a text file.

This method hides all the letters of this word and instead of them shows the symbol "_" (he did not write himself, copied).

public void MakeLabels() { word = GetRandomWord(); char[] chars = word.ToCharArray(); int between = 330 / chars.Length; for (int i = 0; i < chars.Length; i++) { labels.Add(new Label()); labels[i].Location = new Point((i * between) + 10, 80); labels[i].Text = "_"; labels[i].Parent = groupBox1; labels[i].BringToFront(); labels[i].CreateControl(); } } 

Tell me, please, how to make sure that all letters except the first one are hidden.

For example, if the word "SPORTS" was shown "C _ _ _ _".

    4 answers 4

    Zakinu alternative options, once the obvious options listed.

    It is possible through LINQ:

     static string NewWord(string src) { return src[0] + new string(src.Skip(1).Select(x => '_').ToArray()); } 

    Can be regular:

      static string NewWord(string src) { return src[0] + Regex.Replace(src.Substring(1), ".", "_"); } 
    • Since you chose the way of division into separate functions, I would immediately add a parameter for the placeholder with the default value "_" - so that you don’t have to stand up two times when you have to change to another character, say, "X". And so, perhaps, the most beautiful solution from all the proposed so far. - AK
    • If it is necessary to make a space between _ then your version will have to be redone, namely, change Select to SelectMany - Vadim Prokopchuk

    As an option:

     string word = "слово"; string encryptWord = word[0] + (new string('_', word.Length - 1)); 

    The first letter remains, the remaining letters are "replaced" by _

    UPD If you need to make spaces between the first letter and the characters _

     private string HideCharacters(string word) { string res = word[0].ToString(); for (int i = 1; i < word.Length; i++) res += " _"; return res; } 

      I changed the method in question as follows:

        public void MakeLabels() { word = GetRandomWord(); char[] chars = word.ToCharArray(); int between = 330 / chars.Length; labels.Add(new Label()); labels[0].Location = new Point((0* between) + 10, 80); labels[0].Text = chars[0].ToString(); labels[0].Parent = groupBox1; labels[0].BringToFront(); labels[0].CreateControl(); for (int i = 1; i < chars.Length; i++) { labels.Add(new Label()); labels[i].Location = new Point((i * between) + 10, 80); labels[i].Text = "_"; labels[i].Parent = groupBox1; labels[i].BringToFront(); labels[i].CreateControl(); } } 

      As a result, the first letter of the word is initially displayed on the screen, and the rest are replaced with the symbol "_"

      • And do you care about the code that repeats 2 times in a row? And if an error ??? Change in one place, and forget about the second ... - Vadim Prokopchuk

      Fill it with: if(i != 0) .

       for (int i = 0; i < chars.Length; i++) { labels.Add(new Label()); labels[i].Location = new Point((i * between) + 10, 80); if(i!=0)labels[i].Text = "_"; labels[i].Parent = groupBox1; labels[i].BringToFront(); labels[i].CreateControl(); } 
      • Welcome to stackoverflow! Please format the code as a code. Select the code snippet, press Ctrl + K or the {} button on the panel. - AK
      • If conditions look prettier with line breaks, you have everything stuck together to be unreadable. And after all, the letter will not appear at all, add a condition branch. - AK
      • 3
        in your case, the first letter does not appear at all, - Altynbek Tanbayev
      • @AltynbekTanbayev: Well, write the first letter in the first place in this case. - VladD
      • 2
        if(i!=0)labels[i].Text = "_"; It is not true, because does not take into account the first letter at all - Vadim Prokopchuk