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:


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


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


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"
    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';

  • why does it have to be in a for loop?
    – jsotola
    Nov 16, 2022 at 20:03
  • print your code on paper ... get a pencil ... go through your code step by step ... make notes about values of variables ... it may help you discover why your code does not work as you think that it does
    – jsotola
    Nov 16, 2022 at 20:31
  • 2
    The logic of your code seems confused. The loop doing word[i]='_'; belongs the initialization phase, which is performed only when a new game is started. The tests on user_input belong to the regular “play” phase. I suggest you move the “initialize a new game” logic into its own function, and think carefully about what kind of event should trigger the start of a new game. Also, you print() the current guess without erasing the previously printed ones, and without moving the cursor to the next line. Try println() instead. Nov 16, 2022 at 21:05
  • @EdgarBonet could you elaborate more about println() and moving the cursor? Nov 17, 2022 at 4:43
  • @jsotola it is in a for loop because every instance a user inputs a correct character, the for loop should replace the specific index's underscore. Is there a better method to approach this? Nov 17, 2022 at 7:09

1 Answer 1


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;

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;
    if (p == size) {Serial.print( ++wrong ); Serial.println( " wrong guesses");}
    Serial.println (guess);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.