Write a VB.NET program to accept sentences in a text box and count the number of words. Display the count in a message box.

Answer:

Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim str As String

        str = TextBox1.Text

        Dim I, Count As Integer

        Dim arr() As Char = str.ToCharArray

        For I = 0 To arr.Length - 1

            If arr(I) = " " Then

                Count = Count + 1

            End If

        Next

        MsgBox(Count + 1 & " words")

    End Sub

End Class
Scroll to Top