devxlogo

Get the Length of the Longest Word in a String

Get the Length of the Longest Word in a String

LenLongestWord finds the length of the longest word in a stringwhere a word is defined to be bounded by spaces or the ends ofthe string.

It also illustrates a case where a recursive implementation workswell in VB. For example, you would use it to decide how to spreada column heading over multiple lines:

 Function LenLongestWord (ByVal src _        As String) As Integer        Dim i As Integer, j As Integer        i = InStr(src, " ")        If i > 0 Then                j = LenLongestWord(Mid$(src, i _                        + 1))                If j > i - 1 Then _                        LenLongestWord = j Else _                        LenLongestWord = i - 1        Else                LenLongestWord = Len(src)        End IfEnd Function

devx-admin

Share the Post: