c# - Checking if a string exists within a string and if it does change the letter in that string -


i'm creating hangman game. it's 2 player game, 1 player enters word textbox. word displayed question marks.

for example:

the word hello displayed ????? in textbox.

the problem i'm having how change text in textbox when player guesses letter. if player guesses "h" in textbox , enters it, want other text change h????. how that?

you can use following buildmask() method. when text changes in first textbox, add characters hashset, call buildmask, put result second textbox. if want case insensitive, can add iequalitycomparer in hashset constructor, can find solutions if google it.

static void main(string[] args) {     var guessthis = "hello";     var letters = new hashset<char>();      console.writeline(buildmask(guessthis, letters));     letters.add('l');     console.writeline(buildmask(guessthis, letters));     letters.add('h');     console.writeline(buildmask(guessthis, letters)); }  static string buildmask(string toguess, hashset<char> letters) {     var result = new stringbuilder();      foreach (var c in toguess)     {         if (letters.contains(c))             result.append(c);         else             result.append('?');     }      return result.tostring(); } 

Comments