Code:
#include<iostream>
using namespace std;
int get_mask(string word,char letter){
int cnt = 0;
for(int i=0;i<word.length();i++){
if(word[i] == letter){
//Using the
bitwise OR operator
//to form the
binary number
//The power at
index i will be 2*(len-i-1)
cnt = cnt | (1
<< (word.length() - i - 1));
}
}
return cnt;
}
void update_mask(string word,string& mask,char
letter){
//Return if the lengths are not equal
if(word.length() != mask.length())
return;
int position = get_mask(word,letter);
if(position == 0)
return;
int idx = mask.length() - 1;
while(position!=0){
//If the current bit is 1, then if
it is ? in the mask string
//then we can insert the letter at
that index.
if(position & 1){
if(mask[idx] ==
'?')
mask[idx] = letter;
}
idx--;
position = position >>
1;
}
}
bool guessed(string word,string mask){
if(word.length() != mask.length())
return false;
//If all the corresponding letters are same, then
return true
//else return false
for(int i=0;i<word.length();i++){
if(word[i] != mask[i])
return
false;
}
return true;
}
int main(){
string word = "abcfab";
string mask = "??c???";
cout<<"get_mask(word,'a') =
"<<get_mask(word,'a')<<endl;
cout<<"get_mask(word,'e') =
"<<get_mask(word,'e')<<endl;
cout<<"get_mask(word,'f') =
"<<get_mask(word,'f')<<endl;
update_mask(word, mask,'a');
cout<<"update_mask(word, mask,'a') =
"<<mask<<endl;
update_mask(word, mask,'e');
mask = "??c???";
cout<<"update_mask(word, mask,'e') =
"<<mask<<endl;
}
Output:
Please appreciate the solution if you find it helpful.
If you have any doubts in the solution, feel free to ask me in the comment section.
In a single cpp file, provide efficient implementations for the following functions: // Precondition: none //...
Write C programs named mystring.h and mystring.c, containing the headers and implementations of the following functions. int letter_count(char *s) computes and returns the number of English letters in string s. int word_count(char *s) computes and returns the number of words in string s. void lower_case(char *s) changes upper case to lower case of string s. void trim(char *s) removes the unnecessary empty spaces of string s. mystring.h #include <stdio.h> int letter_count(char *); void lower_case(char *); int word_count(char *); void trim(char...
The goal of this task is to reinforce the implementation of container class concepts using linked lists. Specifically, the task is to create an implementation file using a linked list. You need to use the header files, set3.h and node1.h, and the test program, test_set3.cpp. Your documentation must include the efficiency of each function. Please make your program as efficient and reusable as possible. set3.h #ifndef _SET_H #define _SET_H #include <cstdlib> #include <iostream> class set { public: typedef int value_type;...
Create a hangman program. Sample output from your program should look like the following: Current Status for userInputs= _ _ _ _ _ _ Enter next letter: a Current Status for userInputs=a _ _ _ _ _ _ Enter next letter: e Current Status for userInputs=ae _ _ _ _ e _ Enter next letter: i Current Status for userInputs=aei _ _ _ _ e _ Enter next letter: o Current Status for userInputs=aeio _ o _ _ e _...
C++ When running my tests for my char constructor the assertion is coming back false and when printing the string garbage is printing that is different everytime but i dont know where it is wrong Requirements: You CANNOT use the C++ standard string or any other libraries for this assignment, except where specified. You must use your ADT string for the later parts of the assignment. using namespace std; is stricly forbiden. As are any global using statements. Name the...
Do in Python please provide screenshots aswell. Here are the requirements for your game: 1. The computer must select a word at random from the list of avail- able words that was provided in words.txt. The functions for loading the word list and selecting a random word have already been provided for you in ps3 hangman.py. 2. The game must be interactive; the flow of the game should go as follows: • At the start of the game, let the...
For a C program hangman game: Create the function int play_game [play_game ( Game *g )] for a C program hangman game. (The existing code for other functions and the program is below, along with what the function needs to do) (Also the link to program files (hangman.h and library file) is below the existing code section. You can use that to check if the code works) What int play_game needs to do mostly involves calling other functions you've already...
Word Guessing Game Assignment Help Needed. This C++ (I'm using Visual Studio 2015) assignment is actually kind of confusing to me. I keep getting lost in all of the words even though these are supposed to instruct me as to how to do this. Can I please get some help as to where to start? Word guessing game: Overview: Create a game in which the user has a set number of tries to correctly guess a word. I highly recommend...
This is for C programming: You will be given files in the following format: n word1 word2 word3 word4 The first line of the file will be a single integer value n. The integer n will denote the number of words contained within the file. Use this number to initialize an array. Each word will then appear on the next n lines. You can assume that no word is longer than 30 characters. The game will use these words as...
For this lab you will write a Java program that plays a simple Guess The Word game. The program will prompt the user to enter the name of a file containing a list of words. These words mustbe stored in an ArrayList, and the program will not know how many words are in the file before it starts putting them in the list. When all of the words have been read from the file, the program randomly chooses one word...
How to write the insert, search, and remove functions for this hash table program? I'm stuck... This program is written in C++ Hash Tables Hash Table Header File Copy and paste the following code into a header file named HashTable.h Please do not alter this file in any way or you may not receive credit for this lab For this lab, you will implement each of the hash table functions whose prototypes are in HashTable.h. Write these functions in a...