VISUAL BASIC HELP!!! PLEASE!!!

adrenalin

Distinguished
Apr 13, 2004
51
0
18,630
I KNOW IM NOT SUPPOSED TO POST HERE... but im in SERIOUS hurry.

For visual basic program,

I'm supposed to show in a label what common letters are in BOTH of the sentences...

like sentence1 ( hi, man) sentence2 ( hi,mam) then the letters would be H I M A

I know that im supposed to use For...Next.. loop for this but it is honeslty impossible! PLEASE HELP!!!
 

skylane69

Distinguished
Nov 23, 2006
2
0
18,510
The following function does this. All you would need to do is set the return string from this function into the text property of the label. This is code is technically VB6 but I believe this will work just as well in .NET, though there may be a more elegant solution in .NET.

[code:1:35de2561df]
Public Function GetLetters(String1 As String, String2 As String) As String

Dim CommonChars As String
Dim CharNow As String
Dim CharCode As Integer
Dim i As Long

'Initialize
CommonChars = ""

'Loop over every character in string1
For i = 1 To Len(String1)

'Get the current character
CharNow = Mid(String1, i, 1)

'Is this character also in string2?
If InStr(1, String2, CharNow, vbTextCompare) > 0 Then

'We only want letters (lower case letters occur in entries 97 through 122 of the ASCII character set)
CharCode = Asc(LCase(CharNow))
If CharCode >= 97 And CharCode <= 122 Then

'Found a new character - add it
CommonChars = CommonChars & CharNow & " "

End If

End If

Next i

'Return the found strings (using the trim function will get rid of any extra trailing spaces)
GetLetters = Trim(CommonChars)

End Function
[/code:1:35de2561df]

Good luck.