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
See also  Why ChatGPT Is So Important Today
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist