Exercise
Count letters in a file
Objetive
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)
Code
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
Do
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
Next
End If
Loop While line IsNot Nothing
myfile.Close()
Console.WriteLine("Amount of letter: {0}", countLetter)
If debug Then Console.ReadLine()
End Sub
End Class
End Namespace