I have written a program to find the length of last word.Please tell me where I am wrong.
Dim a As String
Console.WriteLine("Enter a string")
a = Console.ReadLine
Dim lastWordLength As Integer
Dim beforeFirstNonEmptyChar As Integer
lastWordLength = 0
beforeFirstNonEmptyChar = True
If a.Length = 0 Then
Console.Write(lastWordLength)
End If
For i = a.Length - 1 To 0
If a(i) = " " Then
lastWordLength = lastWordLength + 1
beforeFirstNonEmptyChar = False
ElseIf beforeFirstNonEmptyChar Then
Exit For
End If
Next
Console.WriteLine(lastWordLength)
Console.ReadLine()
End Sub
2 answers
How do you know there's anything wrong? Have you run it to see whether it works? If not, what happens?
It doesn't work.