Asked by Raj
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
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
Answers
Submit Your Answer
We prioritize human answers over AI answers.
If you are human, and you can answer this question, please submit your answer.