Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
SQL Server

How to Count number of Alphabets in a Text File

Author: DevASP
Download Source Code :

This article is about how to count the number of alphabets in a text file using the System.IO namespace, which provides basic operations on files and directories.

  • Steps you will do.
  • Start visual studio and create a new window application.
  • Set the following properties of the form.
Name = frmMain, Text = Search Alphabets


  • Drop a RichTextBox Control to your form and set its following properties
Name = RTB, Text = “”


  • Drop OpenFileDialog control in your designer and set its name property to ‘OPD’.
  • Drop a Button Control on the form and set the following properties.
Name = btRead, Text = Read File


  • Open the code window.
  • Import System.IO in your project using the following code at the top of your class.
Imports System.IO


  • In the click event of the button write the following code which will count the number of each character in the given file and display in the RichTextBox.


OPD.Filter = "Text files (*.txt)|*.txt"



Dim filepath As String = OPD.FileName


If Not System.IO.File.Exists(filepath) Then


Exit Sub




Dim fileStream As New FileStream(filepath, FileMode.Open, FileAccess.Read)


Dim readerStream As New StreamReader(fileStream)


Dim letter(25) As Integer

Dim strText As String

Dim current As Integer


Dim intLoopStrText, intLoopLetter As Integer



strText = readerStream.ReadToEnd

strText = strText.ToUpper


While (intLoopStrText < strText.Length)

current = Asc(strText.Substring(intLoopStrText, 1))

If current >= 65 And current <= 90 Then


letter(current - 65) += 1


End If


intLoopStrText += 1


End While


For intLoopLetter = 0 To 25

RTB.Text &= Chr(65 + intLoopLetter) & "=" & letter(intLoopLetter) & vbCrLf






Catch ex As IOException




End Try


End If 

Add Article Comment:
Name :
Email Address :
Comments :
<< Make Parent Child Relationship between Forms Using IsMdiContainer Property of the Form

Disclaimer - Privacy
© 2002-2017