I'm working on a debugging assignment and I'm stuck! Can anyone give me a tip on where to start with this? I'm so lost! It is to display the number of words in a sentence then give the average of the characters per sentence and display each individual word in a list box.
Here is my code:
'Determine word count and average letters / word in the sentence
Dim iLength As Integer 'length of string
Dim iWords As Integer 'number of words in sentence
Dim iLetterCount As Integer 'count of letters
Dim sWord As String 'current word
Dim sPrevLetter As String 'previous letter
Dim x As Integer 'loop counter
Dim sLetter As String 'current letter from sentence
Dim sLine As String = txtInput.Text.Trim.ToLower 'entered text - no leading or trailing spaces - make all lowercase
Try
iLength = sLine.Length
If iLength = 0 Then
'nothing entered
MessageBox.Show("Please enter a sentence.")
Else
sWord = 0
sPrevLetter = 0
For x = 0 To iLength
sLetter = sLine.Chars(x) 'get Next Letter
Select Case sLetter
Case "a" To "z"
'we are in a word - increment letter counter
sLetter = sWord
Case Else
'we are in a break between words
If sPrevLetter <> " " Then 'we don't want to count succesive spaces as a word
'add word to list
lstLetters.Items.Add(sWord)
sWord = "" 'reset for next word
End If
End Select
sPrevLetter = sLetter
Next
If sWord <> "" Then
lstLetters.Items.Add(sWord)
End If
lblAverage.Text = "Average letters per word: " & iLetterCount / iLength
lblWordCount.Text = "Words Counted: " & lstLetters.Items.Count
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub