Skip to main content

Hangman game using C programming

 Hangman game using C programming:

Program:

#include <stdio.h> #include <string.h> #include <ctype.h> #define MAX_WRONG_GUESSES 6 #define MAX_WORD_LENGTH 20 int main() { char word[MAX_WORD_LENGTH]; char guessed[MAX_WORD_LENGTH]; int numWrongGuesses = 0; int numCorrectGuesses = 0; int length; int i; char playAgain; do { printf("Enter the word to guess (up to %d characters): ", MAX_WORD_LENGTH - 1); scanf("%s", word); length = strlen(word); // Initialize guessed array with underscores for (i = 0; i < length; i++) { guessed[i] = '_'; } guessed[length] = '\0'; while (numWrongGuesses < MAX_WRONG_GUESSES && numCorrectGuesses < length) { printf("\nWord: %s\n", guessed); printf("Enter a letter: "); char guess; scanf(" %c", &guess); guess = tolower(guess); int found = 0; for (i = 0; i < length; i++) { if (word[i] == guess) { guessed[i] = guess; numCorrectGuesses++; found = 1; } } if (!found) { numWrongGuesses++; printf("Incorrect guess. You have %d guesses left.\n", MAX_WRONG_GUESSES - numWrongGuesses); } } if (numCorrectGuesses == length) { printf("\nCongratulations! You guessed the word: %s\n", word); } else { printf("\nSorry, you ran out of guesses. The word was: %s\n", word); } printf("Do you want to play again? (y/n): "); scanf(" %c", &playAgain); playAgain = tolower(playAgain); // Reset variables for the next game numWrongGuesses = 0; numCorrectGuesses = 0; } while (playAgain == 'y'); return 0; }

Output:

Enter the word to guess (up to 19 characters): game

Word: ____
Enter a letter: g

Word: g___
Enter a letter: a

Word: ga__
Enter a letter: m

Word: gam_
Enter a letter: e

Congratulations! You guessed the word: game
Do you want to play again? (y/n): n


Comments

Popular posts from this blog

Cloudflare Is Down Worldwide: What Happened, What It Means & What You Can Do

🌍 Cloudflare Is Down Worldwide: What Happened & Why the Internet Broke Today On 18 November 2025 , the internet experienced one of the largest global outages in years as Cloudflare , the backbone of countless websites and online services, went down unexpectedly. The outage caused millions of websites to become slow, unreachable, or return 5xx internal server errors , leading to widespread disruption across businesses, apps, and essential online platforms. This blog post explains what happened, why the internet broke for many users, and what Cloudflare has officially said so far. What Exactly Happened? Around 4:30 PM IST , reports began flooding social media and outage trackers like Downdetector. Users across India, Europe, the US, and Southeast Asia noticed that: Websites were not loading Requests were timing out Services dependent on Cloudflare CDN or DNS stopped responding APIs hosted through Cloudflare were failing Even some security and protection layers were ...

JavaProgramForEchoServerAndEchoClient

  Java: Program: EchoServer: import java.io.*; import java.net.*; public class EServer { public static void main(String args[]) { try (ServerSocket s = new ServerSocket(9000); Socket c = s.accept(); DataInputStream is = new DataInputStream(c.getInputStream()); PrintStream ps = new PrintStream(c.getOutputStream())) { while (true) { ps.println(is.readLine()); } } catch (IOException e) { System.out.println(e); } } } EchoClient: import java.io.*; import java.net.*; public class EClient { public static void main(String arg[]) { try (Socket c = new Socket(InetAddress.getLocalHost(), 9000); PrintStream os = new PrintStream(c.getOutputStream()); DataInputStream is = new DataInputStream(System.in); DataInputStream is1 = new DataInputStream(c.getInputStream())) { ...

Unreal Engine Product Showcase: Mesmerizing Video Sequence Render

  4k Image: