Pozor na ty domácí úkoly, neradi to tady vidí, ale je vidět snaha, otestuj si to a uvidíš... Snad i více mezer, ale to by snad nemělo vadit. V modulu :
Option Explicit
Public Type MaxWordsInSentence
Length As Long
Words As New Collection
End Type
Function FindMaxWordInSentence(ByVal sentence As String) As MaxWordsInSentence
'bez tecky na konci
If InStrRev(sentence, ".") = Len(sentence) Then
sentence = Left(sentence, InStrRev(sentence, ".") - 1)
End If
'ukrajovat az do konce
Do While Len(sentence) > 0
If InStr(sentence, Space(1)) > 0 Then
If (InStr(sentence, Space(1)) - 1) > FindMaxWordInSentence.Length Then
FindMaxWordInSentence.Length = InStr(sentence, Space(1)) - 1
While FindMaxWordInSentence.Words.Count > 0
FindMaxWordInSentence.Words.Remove FindMaxWordInSentence.Words.Count
Wend
FindMaxWordInSentence.Words.Add Left(sentence, InStr(sentence, Space(1)) - 1)
ElseIf (InStr(sentence, Space(1)) - 1) = FindMaxWordInSentence.Length Then
FindMaxWordInSentence.Words.Add Left(sentence, InStr(sentence, Space(1)) - 1)
End If
sentence = Mid(sentence, InStr(sentence, Space(1)) + 1)
Else
If Len(sentence) > FindMaxWordInSentence.Length Then
FindMaxWordInSentence.Length = Len(sentence)
While FindMaxWordInSentence.Words.Count > 0
FindMaxWordInSentence.Words.Remove FindMaxWordInSentence.Words.Count
Wend
FindMaxWordInSentence.Words.Add sentence
ElseIf Len(sentence) = FindMaxWordInSentence.Length Then
FindMaxWordInSentence.Words.Add sentence
End If
sentence = ""
End If
Loop
End Function
Sub NajdiNejdelsiSlovoBezPouzitiSplit()
'zadání věty
Dim veta As String
veta = "Tohle je prohledávaná věta k prohledávání."
'vysledek dle funkce
Dim vysledek As MaxWordsInSentence
vysledek = FindMaxWordInSentence(veta)
'zobrazit hlaseni
Dim msg As String
msg = "Věta: " & veta & vbCrLf
msg = msg & "Počet znaků nejdelšího slova: " & vysledek.Length & vbCrLf
If vysledek.Words.Count > 0 Then
msg = msg & "Počet nalezených nejdelších slov: " & vysledek.Words.Count & vbCrLf
msg = msg & IIf(vysledek.Words.Count > 1, "Nalezená slova: ", "Nalezené slovo: ")
Dim i As Integer
For i = 1 To vysledek.Words.Count
msg = msg & IIf(i > 1, ",", "") & vysledek.Words(i)
Next
End If
MsgBox msg
End Sub
|