AutoComplete A Word

I need a function that checks a global collection of words for the best match for auto completing a word that is passed to it  to teh largest number of matchiong common characters. E.g If the collection contains
sheep
ship
dog
elephant
elephants

Input        output
-----       ------
s      -->   sh (since all words with s start with sh)
sh      -->sh(since next chr is not knwon &can be i or e)
shi      -->   ship
d or do  -->   dog
dog      -->   dog    
e, el,ele ,elep etc --> elephant
elephant --> elephant
x-->x ( no match found)


Any ideas on how I should match? I was thinking :

Function autoComplete(word As text)
Dim i
For i = 1 To allWords.Count
     ' The collection is created fresh for each call to this function
     ' Remove all words that dont match
  If (Left(allWords(i), Len(word)) <> word) Then allWords.Remove i
Next i

'only 1 match
If allWords.Count =1 Then
     autoComplete=  allWords(1)
     Exit Function
End If

' Now the collection contains only matches (s->sheep,ship)
' How can I get the the common part (sh) ?

End Function
sambha03Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

yeeeooowwwCommented:
Function autoComplete(word As text)
Dim i, j
For j = 0 To Len(word)
For i = 1 To allWords.Count
    ' The collection is created fresh for each call to this function
    ' Remove all words that dont match
 If (Left(allWords(i), j) <> Left(word, j)) Then allWords.Remove i
Next i
Next j

'only 1 match
If allWords.Count =1 Then
    autoComplete=  allWords(1)
    Exit Function
End If
End Function


This should do it for you..
Basically I am trying to check for one more character after I reduce the collection size by the no of words that match for every iteration of the j loop (which depends on the length of the word) . Ofcourse you might want to list the entire word match and you can't use the function per se, but can modify it to stop iterating the entire length by passing the iteration count to the function and usign that as the j loop's max value instead of length.


eg:

Function autoComplete(word As text)
Dim i
For i = 1 To allWords.Count
    ' The collection is created fresh for each call to this function
    ' Remove all words that dont match
 If (Left(allWords(i), len(word)) <> word) Then allWords.Remove i
Next i
End Function

this should work better if you call the function with the word everytime the user types a character in the text box..etc

don't return anything.. just do a count on the colletion and display all the values there... this should return to you the max string matched.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DocMCommented:

This source code shows you how to provide parameters and an autocomplete feature to your program, just like in Visual Basic! When the user is typing, a listbox appears listing the possible options. They can press the space bar to have the word completed for them, carry on typing, or select another item on the list.
http://www.developerfusion.com/show/360/

0
sambha03Author Commented:
That shld do the trick with some modifications.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.