String and display help
Mostrar comentarios más antiguos
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.
3 comentarios
Chandra Kurniawan
el 5 de Dic. de 2011
Hello,
Would you tell me what is the content of 'hangman.txt'?
And how your program works??
I interested helping you to solve your program.
So, let me know how it's works.
Chris
el 5 de Dic. de 2011
Walter Roberson
el 5 de Dic. de 2011
http://www.mathworks.com/matlabcentral/answers/13205-tutorial-how-to-format-your-question-with-markup
Respuesta aceptada
Más respuestas (1)
Sean de Wolski
el 5 de Dic. de 2011
Use ismember to determine if the letter 'is a member' of the word.
%Sample word, disguised word and letter guess
word = 'signature';
word_disguised = repmat('*',size(word));
letter = 'q'; %try various letters
%Engine
idx = ismember(word,letter); %index of members
if ~any(idx)
disp('Fail Lose an arm!');
else
word_disguised(idx) = letter;
end
Categorías
Más información sobre Text Data Preparation en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!