Skip to content

Commit 0704d92

Browse files
authored
Add Longest Word in Visual Basic (#4506)
1 parent 07c43cb commit 0704d92

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
Public Module LongestWord
2+
Public Sub Main(args As String())
3+
' Check if input is provided
4+
If args.Length = 0 Then
5+
Usage()
6+
Exit Sub
7+
End If
8+
9+
' Join the arguments to form the full input string
10+
Dim multi As String = String.Join(" ", args).Trim()
11+
12+
' Replace newlines, carriage returns, and tabs with spaces
13+
multi = multi.Replace(vbCrLf, " ").Replace(vbCr, " ").Replace(vbLf, " ").Replace(vbTab, " ")
14+
15+
' Split the string into words by spaces, removing empty entries
16+
Dim words As String() = multi.Split(" "c, StringSplitOptions.RemoveEmptyEntries)
17+
18+
' If there are no words, print usage
19+
If words.Length = 0 Then
20+
Usage()
21+
Exit Sub
22+
End If
23+
24+
' Find the longest word length
25+
Dim longestWord As String = ""
26+
Dim maxLength As Integer = 0
27+
28+
' Loop through the words and find the longest
29+
For Each word As String In words
30+
If word.Length > maxLength Then
31+
longestWord = word
32+
maxLength = word.Length
33+
End If
34+
Next
35+
36+
' Output the length of the longest word
37+
System.Console.WriteLine(maxLength)
38+
End Sub
39+
40+
Public Sub Usage()
41+
System.Console.WriteLine("Usage: please provide a string")
42+
End Sub
43+
End Module

0 commit comments

Comments
 (0)