use repl.it intro to C-programing no advance code also put good comment that i can know whats this code use for thank you
use repl.it intro to C-programing no advance code also put good comment that i can know whats this code use for thank you
program for a game of hangman. Store the word to be guessed as individual characters of an array called word. The player must guess the letters belonging to word. If the user enters a valid letter, print a message of encouragement. The program should end when the user successfully guesses the word OR after 15 guesses. Print the contents of the word array at the end of the program.
Hint: Use an array called guessed to keep track of the solution so far. Initialize all elements of guessed to the symbol '*'. Each time a letter in word is guessed, replace the corresponding '*' in guessed with that letter.
Sample execution:
Let's play hangman! Enter a letter: a The letter a is in the word! Enter a letter: m The letter m is in the word! Enter a letter: r The letter r is in the word! The word was: arm
p1
Create the word character array. Get a character from standard
input (the user). Search the
character array (string) for how many times that character appears
by printing a message when the
letter is found. Also print a message if the letter is not
found.
p2
Create the parallel array called guessed. In addition to telling
the user they were correct,
replace the * at the corresponding position of word with the
correctly guessed letter.
p3
Add a terminating condition using a while loop. The easier
condition to implement is a limit of
15 guesses and then the game should terminate and print the array
word.
p4
Add an additional terminating condition to your while loop to
indicate that the word was guessed
correctly. This condition should be based on guessed having no more
* characters in it. Same
as before, if this condition is met, the game should terminate and
print the array word.
p5:
Add to your program the ability for a game master to enter a
word for a user to guess. Your program
should now be able to use a default (hard coded) word or a word
entered by a game master. You can
use a switch case or if statement to let the user select which mode
to play the game in.
code:-
#include <stdio.h>
#include <string.h>
int main() {
// Get word to guess
char answer[128];
printf("lets play hanuman: ");
//fflush(stdout);
scanf(" %s", answer);
// Set the mask array - mask[i] is true if the
// character s[i] has been guessed. The mask
// must be allocated, and initialized to all false
int N = strlen(answer);
int mask[N];
for (int i=0; i < N; ++i) {
mask[i] = 0;
}
// Loop over each round of guessing
int gameover = 0;
while (! gameover) {
// Print word with *s for unguessed letters
printf("Enter the Letter : ");
for(int j=0; j < N; ++j) {
if (mask[j]) {
printf("%c", answer[j]);
}
else {
printf("*");
}
}
printf("\n");
// Get player's next guess
char guess;
printf("Letter? ");
fflush(stdout);
scanf(" %c", &guess);
// Mark true all mask positions corresponding to guess
for(int k=0; k < N; ++k) {
if (answer[k] == guess) {
mask[k] = 1;
}
}
// Determine whether the player has won!
gameover = 1;
for(int m = 0; m < N; ++m) {
if (!mask[m]) {
gameover = 0;
break;
}
}
}
// Print victory message!
printf("the letter \"%s\".\n", answer ,"is in the world");
return 0;
}
output:-
Get Answers For Free
Most questions answered within 1 hours.