[Solved] Hangman,how to replace the underscores of the string in the correct position of the secret string


You should keep a reference of the position of each letter in the string.
You can use the toCharArray method.

    String s = "ciao";
    String underscore = s.replaceAll(".", "_ ").trim();
    char[] sC = s.toCharArray();
    if(s.contains("a"){
      StringBuilder myUnderscore = new StringBuilder(underscore);
      for(int i = 0; i < sC.length; i++){
         if(sc[i] == 'a'){
           myUnderscore.setCharAt(i, 'a');
         }
      }
    }
    myUnderscore.toString();

Hope it helps, it’s my first answer!

2

solved Hangman,how to replace the underscores of the string in the correct position of the secret string