Solve the following Exercises Upload your homework to blackb
Solution
// C++ code guess the word
#include <cstdlib>
 #include <iostream>
 #include <stdlib.h>     /* srand, rand */
 #include <string.h>
 #include <algorithm>
 #include <fstream>
using namespace std;
int main()
 {
 string guess;
 int wrong=1;
 string inputWord;
 string letternotfound;
srand (time(NULL));
 int attempts = rand()%6 + 1;
cout << \"Enter the word to be guessed: \";
 getline(cin, inputWord);
 
 string inputWordcopy = inputWord;
for(int i=0; i!=inputWord.length(); i++)
 {
    if(inputWord.at(i) == \' \')
     {
       letternotfound += \" \";
     }
     else
     {
       letternotfound += \"_\";
     }
 }
 
 cout << endl;
while(1)
 {
     if(wrong == attempts)
     {
       cout << \"\ You Lose!\ Word: \" << inputWord << endl;
       break;
     }
    cout << letternotfound << endl;
     cout << \"You have \" << attempts - wrong << \" attempt left\" << endl;
    if(letternotfound == inputWord)
     {
       cout << \"Well done, You win!\" << endl;
       cout << \"Word :\" << inputWord << endl << endl;
       break;
     }
    cout << \"Guess a letter\" << endl;
     cin >> guess;
   
     if(inputWordcopy.find(guess) != -1)
     {
         while(inputWordcopy.find(guess) != -1)
         {
           letternotfound.replace(inputWordcopy.find(guess), 1, guess);
           inputWordcopy.replace(inputWordcopy.find(guess), 1, \"_\");
         }
     }
    else
     {
       cout << \"Letter not in word\ \" << endl;
     
     }
   
     wrong++;
     cout << endl;
 }
 return 0;
 }
/*
 output:
Enter the word to be guessed: mine
____
 You have 5 attempt left
 Guess a letter
 m
m___
 You have 4 attempt left
 Guess a letter
 q
 Letter not in word
 m___
 You have 3 attempt left
 Guess a letter
 n
m_n_
 You have 2 attempt left
 Guess a letter
 e
m_ne
 You have 1 attempt left
 Guess a letter
 p
 Letter not in word
You Lose!
 Word: mine
 */



