I think that if you are really learning youself Visual Basic, so you should be really try to figure out this?
One solution is to search each postion of a string to find if the string postion value is the same as letter “a” (in your case) and then just add the result into a counter.
Here’s a simple solution, which counts the occurances of “a” in TextBox1 and displays the result in TexbtBox2.
Note that this is a case-sensitive (and hard-coded) solution. This code only calculates the occurrances of letter “a” (not “A”). If you want to count both lower and upper case occurrances, you could try to think about that yourself.
‘
Dim iCount As Integer
Dim iStringLen As Integer
Dim iPos
Dim sString As String
iCount = 0
sString = Text1.Text ‘string given by user
For iPos = 1 To Len(sString)
‘ search each positition of the string
‘the Len() function calculates the length of the string.
‘ Comapre each postion with “a”
If Mid(sString, iPos, 1) = “a” Then
iCount = iCount + 1
End If
Next
‘ Display the result
Text2.Text = iCount
Form1.Refresh
‘
Kaj B.
Discuss This Question: 1  Reply