longest and shortest word?

shambles

New member
Joined
Mar 28, 2009
Messages
2
Programming Experience
Beginner
Hey guys. i need help. for college i have to have a user enter a string and then split the string into seperate words which i have done. however i also need to find the longest and shortest word which i just can't do and its driving me mad. can someone please have a look at my code below and tell me what i've done wrong. Thanks guys

Private Sub btnQ5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnQ5.Click
Dim enterText As String
Dim i As Integer
Dim stringArray() As String
Dim Shortest As String
Dim Longest As String
Dim x As Integer

enterText = InputBox("Please enter some text")

stringArray = enterText.Split(" ")

For i = 0 To stringArray.Length - 1
MsgBox(stringArray(i))
Next

'This is the part i am having trouble with

Shortest = stringArray(LBound(stringArray))
Longest = stringArray(LBound(stringArray))

For x = 0 To stringArray.Length - 1
If Len(Shortest) < Len(stringArray(x)) Then Shortest = stringArray(x)
If Len(Longest) > Len(stringArray(x)) Then Longest = stringArray(x)
Next
lstOut.Items.Add(Shortest)
lstOut.Items.Add(Longest)
End Sub
 
1st off are they really making you use Visual Studio 2005 in a current college class!?

There's several ways you could go about this.

If you were using Visual Studio 2008 you could do it like this. Here I'm sorting by length of the string and then alphabetically because I word of the same length need to be handled as well.

VB.NET:
        Dim inputText As String = "Peter Piper picked a peck of pickled peppers."
        Dim inputArrLinq As String() = inputText.Split(" "c)
        inputArrLinq = inputArrLinq.OrderBy(Function(str) str.Length).ThenBy(Function(str) str).ToArray

You could also use an IComparer to handle this. Once again sorting by length then alphabetically.

VB.NET:
        Dim inputArrIComp As String() = inputText.Split(" "c)
        Array.Sort(inputArrIComp, New StringComparerByLengthThenAlpha)

VB.NET:
Public Class StringComparerByLengthThenAlpha
    Implements IComparer(Of String)

    Public Function Compare(ByVal x As String, ByVal y As String) As Integer Implements System.Collections.Generic.IComparer(Of String).Compare
        If x.Length > y.Length Then
            Return 1
        ElseIf x.Length < y.Length Then
            Return -1
        Else
            Return String.Compare(x, y)
        End If
    End Function
End Class

In both of these instances once they're sorted you can just take the 1st and last elements of the array and have your shortest and longest words.

A third option would be to initialize a variable for the longest and shortest words and initialize them to the first word in your array. Then you can loop through the array and check whether the new word is longer/shorter than your currently saved values and swap them if the condition is me. I haven't included any logic for if they're the same length and instead keep the existing word. You can add this logic yourself if that's what you're looking for.

VB.NET:
        Dim inputArrManual As String() = inputText.Split(" "c)
        Dim shortWord As String = inputArrManual(0)
        Dim longWord As String = inputArrManual(0)
        For i As Integer = 0 To inputArrManual.Length - 1
            If inputArrManual(i).Length < shortWord.Length Then
                shortWord = inputArrManual(i)
            End If
            If inputArrManual(i).Length > longWord.Length Then
                longWord = inputArrManual(i)
            End If
        Next
 
Please turn Option Strict On.

You need to add the c suffix after the " " in the Split method with Option Strict On.

Your program works except it reverses the longest and shortest words. All you have to do is reverse the > and < operators.


VB.NET:
		Dim enterText As String
		Dim i As Integer
		Dim stringArray() As String
		Dim Shortest As String
		Dim Longest As String
		Dim x As Integer

		enterText = InputBox("Please enter some text")

		stringArray = enterText.Split(" "c)
		'add the c suffix to denote a character

		For i = 0 To stringArray.Length - 1
			MsgBox(stringArray(i))
		Next

		'You needed to reverse the > and < operators

		Shortest = stringArray(LBound(stringArray))
		Longest = stringArray(LBound(stringArray))

		For x = 0 To stringArray.Length - 1
			If Len(Shortest) > Len(stringArray(x)) Then Shortest = stringArray(x)
			If Len(Longest) < Len(stringArray(x)) Then Longest = stringArray(x)
		Next
		lstOut.Items.Add("Shortest = " & Shortest)
		lstOut.Items.Add("Longest = " & Longest)



A problem will occur if the user enters more than one space between words. The resulting empty word will be selected as the shortest. You could program a loop to replace each double space with a single space until no more double spaces have been found. Place this before the Split method:

VB.NET:
		Do	
                    enterText = enterText.Replace("  ", " ")
		Loop While enterText.Contains("  ")
 
For-Each is usually more readable than For-Next.
VB.NET:
Dim input As String = "Peter, Piper  picked a peck of 'pickled' peppers."
Dim words As String() = input.Split(" .,:;'""".ToCharArray, StringSplitOptions.RemoveEmptyEntries)
Dim shortest As String = words(0)
Dim longest As String = words(0)

For Each word As String In words

    If word.Length < shortest.Length Then shortest = word

    If word.Length > longest.Length Then longest = word            

Next
 
This is the way to solve your problem

Public Class Form1
    Private Sub cmdFInd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdFInd.Click
        Dim s, temp, min, max As String
        Dim i, count, n As Integer
        count = 0
        s = CStr(input.Text)
        s = s & " "
        For i = 0 To s.Length - 1
            If Char.IsLetterOrDigit(s.Chars(i)) Then
                count = count + 1
                temp = temp & s.Chars(i)
            ElseIf Char.IsWhiteSpace(s.Chars(i)) Then
                If n = 0 Then
                    n = count
                    count = 0
                    min = temp
                    max = temp
                    temp = Nothing
                ElseIf count > n Then
                    If max.Length < count Then
                        count = 0
                        max = temp
                        temp = Nothing
                    End If
                ElseIf count < n Then
                    If min.Length > count Then
                        count = 0
                        min = temp
                        temp = Nothing
                    End If
                ElseIf count = n Then
                    If min.Length = temp.Length Then
                        count = 0
                        min = temp
                        temp = Nothing
                    ElseIf max.Length = temp.Length Then
                        count = 0
                        max = temp
                        temp = Nothing
                    End If
                End If
                temp = Nothing
                count = 0
            End If
        Next
        output.Text = "The largest word is " & max & vbCrLf & "The smallest word is " & min
    End Sub
End Class
 
Last edited by a moderator:
Back
Top