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

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.

Based on the code you provided, it seems like you are attempting to find the length of the last word in a given string. While your approach is on the right track, there are a couple of errors that need to be addressed.

1. Issue in the if-else statement:
- Your `if` statement checks if the input string has a length of zero. If true, it prints the `lastWordLength`. However, you haven't included an `else` statement to handle cases where the input string is not empty. You should add an `else` statement to appropriately handle such cases.

2. Issue in the for loop:
- Your loop starts at `a.Length - 1` and goes down to 0. However, the loop should stop when it reaches the first non-empty character before the last word. Instead of using the condition `beforeFirstNonEmptyChar`, you can stop the loop when `i` becomes less than 0.

Here is an updated version of your code:

```vb
Dim a As String
Console.WriteLine("Enter a string")
a = Console.ReadLine

Dim lastWordLength As Integer = 0
Dim foundLastWord As Boolean = False

If a.Length = 0 Then
Console.Write(lastWordLength)
Else
For i = a.Length - 1 To 0 Step -1
If a(i) = " " Then
If foundLastWord Then
Exit For
End If
Else
lastWordLength += 1
foundLastWord = True
End If
Next

Console.WriteLine(lastWordLength)
End If

Console.ReadLine()
```

In this updated code, I have added an `else` statement to handle the case when the input string is not empty. Inside the loop, I check if the current character is a space. If it is, I check if we have already found the last word. If true, we exit the loop. Otherwise, if the character is not a space, we increment the `lastWordLength` and set `foundLastWord` to true.

Please give the updated code a try and let me know if you encounter any further issues.