Hello,

I am trying to make a hangman game and i've almost completed it except the main part. When a user clicks a botton containing an alphabet, how to show that specific text on the blank part.

-------------------------------------------------------------------------------
Here's an example:

_D_N_W_B ----->EDANIWEB

so, when the user clicks the button containing the letter "E" i need to replace the '_' with 'E' in both 1st and 7th position.

Thanks in Advance.......

I'll assume you have the answer stored as either a strng or an array. Once the letter key is pressed, search the array or string for the selected letter and find its locations.
Once you have those, go to your board that displays the '_' and replace the characters at those positions.
For your example you would search the array (E,D,A,N,I,W,E,B) and get the locations 0 and 6.
So, in your displayed answer you would replace characters 0 and 6 with the 'E'.

Well i have already tried that bu it gave only the "0" position not "6".

Some pseudocodes would be a great help.

thanks,

Iterate through whole your original string(the one to find).
If char by user is equal, change the '_' in that position in the string on the screen.
Should work for any number of chars.
Happy computing :)

Well, i tried it iterating over the characters in a string but it gave single index of the repeated characters.

but it gave single index of the repeated characters.

Of course it did.
Now while still in your loop change the char in the index of the string to show.
Continue looping 'till the end of the string to find.
Again if a match is found, change the char in the index of the string to show. Etc.

This article has been dead for over six months. Start a new discussion instead.