MATLAB: String and display help

displaystrings

clear all clc
fid = fopen('hangman.txt', 'r'); if fid < 0, error('Cannot open file.'); end CC = textscan(fid, '%s'); C = CC{1}; fclose(fid); index = ceil(rand * numel(C)); word = C{index};
original='Hello World'; Word = word; Word(~isspace(Word)) = '*'
first_guess=char(input('Guess a letter \n','s'));
result1 = ~isempty(findstr(word,first_guess));
selected = first_guess; stars(original == first_guess) = first_guess
OUTPUT Word =
*******
Guess a letter i
stars =
Empty string: 1-by-0
The word is signature, but I don't unerstand how it outputs an empty string. Also, how would I get the letter to replace the asterisk after each guess? Would I end up using a while loop to keep the user guessing until the word is uncovered? This is a lot harder than I thought it would be.

Best Answer

Hello,
So long time you have waiting. Here I give you my code.
I have 'Hangman.txt'
signature
chair
calculator
window
picture
browser
phone
book
table
glass
And this is my code :
clear; clc;
fid = fopen('Hangman.txt','r');
if fid < 0, error('Cannot open file'); end
data = textscan(fid,'%s');
data = data{1};
fclose(fid);
index = ceil(rand * numel(data));
word = data{index};
masked = word;
masked(~isspace(masked)) = '#';
complete = 0;
while complete == 0
clc;
fprintf('Word : %s\n',masked);
letter = char(input('Guess a letter : ','s'));
stat = findstr(word,letter);
if ~isempty(stat)
masked(stat) = letter;
end
if isempty(findstr(masked,'#'))
complete = 1;
end
end
clc; fprintf('Word : %s\n',masked);
Just try to run it.
Related Question