PDA

View Full Version : Word Finder Help on VB!


lamlamlamiaaa
May 7, 2012, 03:07 PM
So far, I made a program where the user enters a sentence in one textbox and then the user enters a word they want to find in that sentence in another text box. After clicking the button, the program will show the word in capital letters. But, the problem is that, it only capitalizes the first word it finds. For example, "Hi my name is visual basic and I love VIsual basic" IF the user wants to find "Visual" it only capitalizes the first visual and not the second one. I need it so that it does that. Here is my code so far :


Dim sentence As String
Dim word As String
Dim location As String
Dim new_sentence As String

sentence = TextBox1.Text
word = TextBox2.Text
location = InStr(sentence, word)
found = found + 1

new_sentence = Mid(sentence, 1, location - 1) & UCase(word) & Mid(sentence, location + Len(word))
Label3.Text = new_sentence


It would be lots of help!
Thank you (;

jsblume
Sep 28, 2012, 11:21 AM
The problem is that the way the InStr function is being used you are always finding only the first. What you need is a loop

You don't need the new_sentence variable. Notice inside the While-Wend loop that I call the InStr function with three arguments instead of two. When the first argument is a number, then it indicates the starting position in the string for the search.

Dim sentence As String
Dim word As String
Dim location As String
Dim new_sentence As String

sentence = TextBox1.Text
word = TextBox2.Text
location = InStr(sentence, word)
found = 0

while location > 0

found += 1

sentence = Mid(sentence, 1, location - 1) & UCase(word) & Mid(sentence, location + Len(word))

location = InStr(location + 1, sentence, word)

wend

Label3.Text = sentence