Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

4 Hangman (20 points) Write an interactive program that plays a game of hangman.

ID: 3728633 • Letter: 4

Question

4 Hangman (20 points) Write an interactive program that plays a game of hangman. Store the characters of the word to be gues array of type char (you can initialize your character array at declaration). Words are seven letters long. Initiall the program displays the length of the word to be guessed. This is in the form of successive stars The player guesses letters belonging to the secret word one by one. After each guess, the letters that have been guessed and the number of wrong guesses are displayed on screen. Your program should terminate when either the entire word is guessed or 4 incorrect guesses have been attempted Your program must be modular. Create at least two meaningful functions that abstract details such as printing the word state after a letter guess is attempted or searching for a letter within a word. Test your program for the words: abandon, annoyed, finance, aerobic, inferno, infancy. Sample execution for word abandon: Hi, let's play hangman. The secret word is: Guess a letter:t Letter t is not part of the secret word, You have 3 attempts left. Guess a letter:a Letter a exists 2 times in the secret word, You have 3 attempts left. Guess a letter:e Letter e is not part of the secret word, you have 2 attempts left. For this problem, submit your pseudocode Submit your c files named hw6.pl.c, hw6.p2.c, hw6.p3.c, and hw6.p3.c via D2L dropbox

Explanation / Answer

The code is pretty simple and obvious, ask in comments if you have any doubts

code.c

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

char words[6][8] = {"abandon", "annoyed", "finance", "aerobic", "inferno", "infancy"};
char word[8] = "*******";
int chosen_word;

void updWord(char found){
   int i, count = 0;
   for(i = 0; i < 7; i++)
       if(words[chosen_word][i] == found)
           word[i] = found;
}
void printWord(){
   printf("%s ", word);
}  

int searchchar(char search, char str[]){
   int i, ret = 0;
   for(i = 0; i < 7; i++)
       if(str[i] == search)
           ret++;
   return ret;
}

int main()
{
   srand(time(NULL));

   int guesses = 4, tmp;
   printf("Hi, let's play a hangman. The secret word is: ");

   chosen_word = rand() % 6;

   char c;
   while(guesses && searchchar('*', word)){
       printWord();
       printf("Guess a letter:");
       scanf(" %c", &c);
       // getchar();

       tmp = searchchar(c, words[chosen_word]);
       if(!tmp){
           guesses--;
           printf("Letter %c is not part of the secret word, you have %d attempts left. ", c, guesses);
       } else{
           updWord(c);
           printf("Letter %c exists %d times in the secret word, you have %d attempts left. ", c, tmp, guesses);
       }
   }

   if(guesses)
       printf("You won! ");
   else
       printf("Sorry you lost, the word is %s ", words[chosen_word]);

   return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote