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

Stable Diffusion WebUI 1.10.1 Full Installation Guide | AUTOMATIC1111 | Windows 11

Stable Diffusion WebUI 1.10.1 Full Installation Guide | AUTOMATIC1111 | Windows 11  Welcome to this step-by-step Stable Diffusion WebUI 1.10.1 installation guide! In this tutorial, we will walk you through the complete setup process on Windows 11 , including downloading and installing Git , setting up Python 3.10.6 , cloning the AUTOMATIC1111 repository , and configuring .gitignore for a clean and efficient installation. By following this guide, you’ll be able to generate AI-generated images using Stable Diffusion with ease. Whether you're new to AI image generation or an experienced user, this guide ensures that your setup is optimized for performance and stability. 🔗 Required Downloads: Before we begin, make sure to download the following tools: ✅ Git for Windows – Download Here ✅ Stable Diffusion WebUI (AUTOMATIC1111) – Download Here ✅ Python 3.10.6 – Download Here 🛠️ Step-by-Step Installation Process 1️⃣ Install Git for Windows Git is required to clone the ...

Unreal Engine Product Showcase: Mesmerizing Video Sequence Render

  4k Image:

Install TensorFlow on Windows 11: Step-by-Step Guide for CPU & GPU

 --- Installing **TensorFlow on Windows 11** requires setting up system dependencies, configuring Python, and ensuring compatibility with CPU or GPU acceleration. This step-by-step guide provides everything needed to install **TensorFlow 2.10 or lower** on **Windows Native**, including software prerequisites, Microsoft Visual C++ Redistributable installation, Miniconda setup, GPU driver configuration, and verification steps.   ### **System Requirements:**   Before installing TensorFlow, ensure your system meets these requirements:   - **Operating System:** Windows 7 or higher (64-bit)   - **Python Version:** 3.9–3.12   - **pip Version:** 19.0 or higher for Linux and Windows, 20.3 or higher for macOS   - **Microsoft Visual C++ Redistributable:** Required for Windows Native   - **Long Paths Enabled:** Ensure long paths are enabled in Windows settings   For **GPU support**, install:   - **NVIDIA ...