Count letters in a file Learn programming Visual Basic (


File Management


Count letters in a file


Create a program to count the amount of times that a certain character is inside a file (of any kind).

The file and the letter can be asked to the user or passed as parameters:

count example.txt a

It must display in screen the amount of letters found.

(you can choose any way to interact with the user, showing the proper help)


Imports System
Imports System.IO
Namespace ConsoleApplication1
    Class CountLetters
        Private Shared Sub Main()
            Dim debug As Boolean = True
            Console.Write("Name of file: ")
            Dim nameFile As String = Console.ReadLine()
            Console.Write("Letter for count: ")
            Dim letter As String = Console.ReadLine()
            Dim myfile As StreamReader
            myfile = File.OpenText(nameFile)
            Dim line As String
            Dim countLetter As Integer = 0

                line = myfile.ReadLine()

                If line IsNot Nothing Then

                    For i As Integer = 0 To line.Length - 1
                        If line.Substring(i, 1) = letter Then countLetter += 1
                End If
            Loop While line IsNot Nothing

            Console.WriteLine("Amount of letter: {0}", countLetter)
            If debug Then Console.ReadLine()
        End Sub
    End Class
End Namespace

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