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

How to Count number of Alphabets in a Text File

Author: DevASP
Download Source Code : 314_CountCharacter.zip

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.

RTB.Clear()

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

OPD.ShowDialog()

 

Dim filepath As String = OPD.FileName

 

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

 

Exit Sub

 

Else

 

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

 

Try

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

 

Next

 

readerStream.Close()

 

Catch ex As IOException

 

MsgBox(ex.Message)

 

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 DevASP.net