# Finding a Certain Letter

**URL:** <https://forum.xojo.com/t/finding-a-certain-letter/37807>\
**Category:** Getting Started\
**Created:** [May 24, 2017, 2:51pm UTC](https://forum.xojo.com/t/finding-a-certain-letter/37807 "2017-05-24T14:51:02Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Arnav\_Sharma](https://forum.xojo.com/letter_avatar_proxy/v4/letter/a/67e7ee/32.png) [@Arnav\_Sharma](https://forum.xojo.com/u/Arnav_Sharma)\
**Post date:** [May 24, 2017, 2:51pm UTC](https://forum.xojo.com/t/finding-a-certain-letter/37807/1 "2017-05-24T14:51:02Z")

</div>

Hi all,

I am a student working on a hangman game for my final exam and I was having some issues with my string manipulation. As you know, in hangman, if there are two or more of one letter, and that letter is chosen, every position that holds that letter is enabled. I was having some issues with the length of my code regarding this.  
For a two letter word, I have it set it up in a control set so that

`  
//other code up here 
 elseif (len(app.SecretWord) = 2) then
    if (me.caption = left(app.SecretWord, 1)) then
      lb_firstLetter.Text = me.caption
    elseif (me.caption = mid(app.SecretWord, 2)) then
      lb_secondLetter.Text = me.caption
    elseif (me.caption <> left(app.SecretWord, 1)) or (me.caption <> mid(app.SecretWord, 2)) then
      MsgBox("error - check your code")
    end if
if (left(app.SecretWord, 1) = mid(app.SecretWord, 2, 1)) and(me.caption = left(app.SecretWord, 1)) and (me.caption = mid(app.SecretWord, 2, 1)) then
      lb_firstLetter.text = me.caption
      lb_secondLetter.text = me.caption
    end if`  
Whereas `app.SecretWord ` is an app property that the user input  
The problem with my code is that when I have words that are longer, such as words with a length of 10, I would have to put in many more lines of code. Additionally, there could be more than two of the same letter, creating even more combinations and more lines of code.  
Ultimately, my question is: Is there some way to manipulate the string so that it picks out a certain letter from my app property?

---

<div class="post-metadata">

**Author:** ![Norman\_P](https://forum.xojo.com/letter_avatar_proxy/v4/letter/n/858c86/32.png) [@Norman\_P](https://forum.xojo.com/u/Norman_P)\
**Post date:** [May 24, 2017, 3:14pm UTC](https://forum.xojo.com/t/finding-a-certain-letter/37807/2 "2017-05-24T15:14:17Z")

</div>

Have a peek at [INSTR](http://documentation.xojo.com/index.php/InStr)  
That one you can use to find every letter in whatever string a person enters

---

<div class="post-metadata">

**Author:** ![Markus\_Winter](https://forum.xojo.com/user_avatar/forum.xojo.com/markus_winter/32/144_2.png) [@Markus\_Winter](https://forum.xojo.com/u/Markus_Winter)\
**Post date:** [May 24, 2017, 4:24pm UTC](https://forum.xojo.com/t/finding-a-certain-letter/37807/3 "2017-05-24T16:24:50Z")

</div>

Better:

Setup:

make a custom canvas with a boolean ShowLetter and a string property LetterToShow  
In the paint event draw the letter if ShowLetter is true  
Add one custom canvas left of the window (make sure it stays out of the way)  
Add an array of CustomCanvas to your window  
Add the custom canvas to the array -\> it has the index 0 !!!

Take a word.  
Use split to turn it into an array of string  
Iterate over the array: for each letter add a CustomCanvas to the window and set its LetterToShow property to the letter  
Add each CustomCanvas to the array -\> has index 1 to the end !!!

Play:

User chooses letter

Iterate over the CustomCanvas array from 1 !!! to the end.  
If letter = LetterToShow then set boolean ShowLetter to true

Invalidate the canvas

---

<div class="post-metadata">

**Author:** ![Arnav\_Sharma](https://forum.xojo.com/letter_avatar_proxy/v4/letter/a/67e7ee/32.png) [@Arnav\_Sharma](https://forum.xojo.com/u/Arnav_Sharma)\
**Post date:** [May 26, 2017, 4:28pm UTC](https://forum.xojo.com/t/finding-a-certain-letter/37807/4 "2017-05-26T16:28:53Z")

</div>

@Norman Palardy I took a look at InStr, but I am having trouble understanding how I would equate an InStr integer to a letter to be displayed.

---

<div class="post-metadata">

**Author:** ![Robert\_Weaver](https://forum.xojo.com/letter_avatar_proxy/v4/letter/r/919ad9/32.png) [@Robert\_Weaver](https://forum.xojo.com/u/Robert_Weaver)\
**Post date:** [May 27, 2017, 8:33am UTC](https://forum.xojo.com/t/finding-a-certain-letter/37807/5 "2017-05-27T08:33:14Z")

</div>

Whenever the player guesses a letter, append it to a string that contains all of the guessed letters so far:

```auto
guessedLetters = guessedLetters + newGuess
```

Then, to update the displayed word, loop through the characters of the secret word one at a time

```auto
dim outputString as string = ""
for i as integer = 1 to len(secretword)
  dim currentcharacter as string = mid(secretword,i,1)
  'See if the current character is one that has been already guessed
  if instr(guessedLetters, currentcharacter)>0 then
    'Yes so append it to the output
    outputString = outputString + currentcharacter
  else
    'No, hasn't been guessed, so append a hyphen to the output
    outputString = outputString + "-"
  end if
next
' Now check if all the letters have been found
if outputString = secretword then
  MsgBox "You Win!!!"
end if
```

I haven’t tested this, so there may be bugs.

---

<div class="post-metadata">

**Author:** ![Emile\_Schwarz](https://forum.xojo.com/letter_avatar_proxy/v4/letter/e/90ced4/32.png) [@Emile\_Schwarz](https://forum.xojo.com/u/Emile_Schwarz)\
**Post date:** [May 27, 2017, 9:05am UTC](https://forum.xojo.com/t/finding-a-certain-letter/37807/6 "2017-05-27T09:05:41Z")

</div>

If your application is slow, you can change these:

1. Never compute the len of something in the For line of a loop

[code]Dim secretWordLen As Integer  
Dim i As Integer

For i = 1 To Len(secretWordLen)[/code]

1. String Concatenation is slow, so avoid:

```auto
   outputString = outputString + currentcharacter
```

Prefer the better:

` Dim outputString() As String
    outputString.Append currentcharacter`

When done / when you want to use the string you store, use

```auto
   outputString.Join() // Check if () is mandatory
```

---

<div class="post-metadata">

**Author:** ![system](https://forum.xojo.com/uploads/default/original/1X/455b4dcc0e61630e9f81940004ddffa49c432a46.png) [@system](https://forum.xojo.com/u/system)\
**Post date:** [October 29, 2020, 6:46pm UTC](https://forum.xojo.com/t/finding-a-certain-letter/37807/7 "2020-10-29T18:46:58Z")

</div>


