How can I replace an underscore with a letter if an input character matches that letter?

7 views (last 30 days)
So for a project I'm working on the user is shown a number of underscores (their hint) equal to the length of a hidden word (selected_word here) and they have to guess the letters in the word. I've gotten as far as displaying the proper number of underscores using the repmat function but after the user makes their guess, I can't figure out how to replace the underscores in the locations of the corresponding matching letters with their guess, assuming they guess correctly. I've tried using repmat but that didn't work and I've considered using regexprep but I can't figure out how to tell it to replace all characters that DON'T match the users guess with an underscore. What I've tried that hasn't worked is commented out. Here are just the important parts of what I have:
hint=repmat('_ ',1,length(selected_word));
guess=input('6 attempt(s) remaining. Enter a new guess:','s');
if findstr(guess,selected_word)>0
correct=findstr(guess,selected_word);
%hint=repmat(guess,1,correct);
%hint=repmat(guess,1,hint(1,correct));

Answers (1)

Guillaume
Guillaume on 23 Apr 2015
As per the documentation of findstr, don't use it, use strfind instead.
You're complicating things. Assuming the user has entered a single letter, you can just use element-wise comparison ( ==) to find which letter of your selected_word matches the entered letter. You then simply use the logical array generated by this comparison to replace your underscores:
guess = input(sprintf('%d attempt(s) remaining. Enter a new guess:', numattempts), 's');
%add check that a single letter has been entered!
ismatch = selected_word == guess; %compare each letter of selected_word to guess
hint(ismatch) = guess; %replace match by actual letter
I would add a check that a single letter has been entered, otherwise the user could just enter words.
You probably also want to convert entries to uppercase or lowercase to remove ambiguity on the casing.
  2 Comments
Mel Smith
Mel Smith on 25 Apr 2015
So I used strfind and ended up doing what I have below, but the only problem is that it can't replace more than one underscore (for example, if the word had two "e"s). Ignore the used_letters and attempts. I'll add in the single letter and case-sensitive checks after I've figured this problem out. :/
prompt=sprintf('%.0f attempt(s) remaining. Enter a new guess: ',attempts);
guess=input(prompt,'s');
if isempty(strfind(selected_word,guess))%If user guesses wrong.
used_letters=cat(2,used_letters,guess);
fprintf('Hint: %s\nUsed letters: {%s}\n',hint,used_letters)
attempts=attempts-1;
else
for letter=1:length(selected_word) %If user guesses correctly.
if strfind(selected_word,guess)==letter
hint(letter)=guess;
Stephen23
Stephen23 on 25 Apr 2015
If you read Guillaume's answer you will notice that they did not use strfind at all, and recommended that you use much simpler logical indexing. This would simplify your code considerably.

Sign in to comment.

Categories

Find more on Characters and Strings in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!