user87488: I am developing a hangman game on tinyduino(tinycircuits) and I am currently stuck on how to properly replace a char based on user input within a for loop. My current code does actually replace the correct underscore(blank), if a user inputs a correct letter.
As per the game hangman, it will print out a series of blanks(in my case, underscores) based on number of letters, that serve as placeholders for the correct letter and will reveal the letter only if a user inputs a correct letter.
However, whenever a user inputs another correct letter (after inputting a correct letter prior), my program creates another 'line' and replaces the underscore in that new line instead.
For example, say the word to guess is kotlin. If user inputs 'k', my console shows me:
k_____

The output above is still correct.
But, when the user inputs another letter, say 'o' which is also correct, the result I get is:
k______o____

To further re-emphasize, now if a user inputs 't', I would get:
k______o______t___

What do I need to modify in my code in order to replace the underscores in the original line whenever a user enters a correct letter? Below is my current code.
if (k == 1) { // Str1 is "kotlin"
    display.setCursor(10,50);
    length = strlen(Str1);

    String user_input = String(phone_input);
    String word = String(Str1);

    for (i = 0; i < length; i++) {
      word[i] = '_';
      
      if (user_input == "k") {
        word[0] = 'k';
      }

      if (user_input == "o") {
        word[1] = 'o';
      }

      if (user_input == "t") {
        word[2] = 't';
      }

      if (user_input == "l") {
        word[3] = 'l';
      }

      if (user_input == "i") {
        word[4] = 'i';
      }

      if (user_input == "n") {
        word[5] = 'n';
      }

      display.print(word[i]);
      SerialMonitorInterface.print(word[i]);
    }
  }


user16780: Attention  spoiler (If it's ok for you just to enhance it to a playable version by enhancing the shortcuts! )
If you want to replace single characters in a word, String and
std::string is way too much overhead. Use good old char [] instead.
const char* result { "kotlin" }; // shortcut
const size_t size = 6; // shortcut

char guess[size+1]; // The guessed intermediate result

const byte wrongMax = 22; 
byte wrong = 0; // number of wrong guesses

void setup() {
  for (char& c : guess) c='_';
  guess[size] = 0;
  
  Serial.begin(9600);
  Serial.println(guess);
}

void loop() {
  if (strcmp(result,guess)== 0) {
    Serial.println("Got it !"); while (1){} // Stop, reset required
  }
  if (wrong >= wrongMax) {
    Serial.println("Failed !"); while (1){} // Stop, reset required
  }

  int c = Serial.read();
  if (c >= 'a' && c <= 'z') {
    // shortcut: only lower characters allowed
    byte p;
    for (p=0; p < size; p++) {
       if (c == result[p] && guess[p] == '_') {
         // found a new correct one
         guess[p] = c;
         break;
       }
    }
    if (p == size) {Serial.print( ++wrong ); Serial.println( " wrong guesses");}
    Serial.println (guess);
  }
}


87488: <p>I am developing a hangman game on tinyduino(tinycircuits) and I am currently stuck on how to <strong>properly</strong> replace a <code>char</code> based on user input within a <code>for loop</code>. My current code does actually replace the correct underscore(blank), if a user inputs a correct letter.</p>
<p>As per the game hangman, it will print out a series of blanks(in my case, underscores) based on number of letters, that serve as placeholders for the correct letter and will reveal the letter only if a user inputs a correct letter.</p>
<p>However, whenever a user inputs another correct letter (after inputting a correct letter prior), my program creates another 'line' and replaces the underscore in that new line instead.</p>
<p>For example, say the word to guess is kotlin. If user inputs 'k', my console shows me:</p>
<pre><code>k_____
</code></pre>
<p>The output above is still correct.
But, when the user inputs another letter, say 'o' which is also correct, the result I get is:</p>
<pre><code>k______o____
</code></pre>
<p>To further re-emphasize, now if a user inputs 't', I would get:</p>
<pre><code>k______o______t___
</code></pre>
<p>What do I need to modify in my code in order to replace the underscores in the original line whenever a user enters a correct letter? Below is my current code.</p>
<pre><code>if (k == 1) { // Str1 is &quot;kotlin&quot;
    display.setCursor(10,50);
    length = strlen(Str1);

    String user_input = String(phone_input);
    String word = String(Str1);

    for (i = 0; i &lt; length; i++) {
      word[i] = '_';
      
      if (user_input == &quot;k&quot;) {
        word[0] = 'k';
      }

      if (user_input == &quot;o&quot;) {
        word[1] = 'o';
      }

      if (user_input == &quot;t&quot;) {
        word[2] = 't';
      }

      if (user_input == &quot;l&quot;) {
        word[3] = 'l';
      }

      if (user_input == &quot;i&quot;) {
        word[4] = 'i';
      }

      if (user_input == &quot;n&quot;) {
        word[5] = 'n';
      }

      display.print(word[i]);
      SerialMonitorInterface.print(word[i]);
    }
  }
</code></pre>
 16780: <p>Attention  spoiler (If it's ok for you just to enhance it to a playable version by enhancing the shortcuts! )</p>
<p>If you want to replace single characters in a word, <code>String</code> and
<code>std::string</code> is way too much overhead. Use good old char [] instead.</p>
<pre><code>const char* result { &quot;kotlin&quot; }; // shortcut
const size_t size = 6; // shortcut

char guess[size+1]; // The guessed intermediate result

const byte wrongMax = 22; 
byte wrong = 0; // number of wrong guesses

void setup() {
  for (char&amp; c : guess) c='_';
  guess[size] = 0;
  
  Serial.begin(9600);
  Serial.println(guess);
}

void loop() {
  if (strcmp(result,guess)== 0) {
    Serial.println(&quot;Got it !&quot;); while (1){} // Stop, reset required
  }
  if (wrong &gt;= wrongMax) {
    Serial.println(&quot;Failed !&quot;); while (1){} // Stop, reset required
  }

  int c = Serial.read();
  if (c &gt;= 'a' &amp;&amp; c &lt;= 'z') {
    // shortcut: only lower characters allowed
    byte p;
    for (p=0; p &lt; size; p++) {
       if (c == result[p] &amp;&amp; guess[p] == '_') {
         // found a new correct one
         guess[p] = c;
         break;
       }
    }
    if (p == size) {Serial.print( ++wrong ); Serial.println( &quot; wrong guesses&quot;);}
    Serial.println (guess);
  }
}
</code></pre>
