Count words Learn programming Visual Basic (VB.net)

Lesson:

File Management


Exercise:

Count words


Objetive:

Create a Visual Basic (VB.net) program to count the amount of words stored in a text file


Code:

Imports System
Imports System.IO
Class Program
    Private Shared Sub Main(ByVal args As String())
        Dim file As StreamReader = File.OpenText("1.cs")
        Dim line As String
        Dim amountOfWords As Integer = 0

        Do
            line = file.ReadLine()

            If line IsNot Nothing Then
                Dim words As String() = line.Split(" "c)
                amountOfWords += words.Length
            End If
        Loop While line IsNot Nothing

        file.Close()
        Console.WriteLine("Words: " & amountOfWords)
    End Sub
End Class

Juan A. Ripoll - Systems Tutorials and Programming Courses ©  All rights reserved.  Legal Conditions.