Visual Basic 2005-2

0 pts.
Tags:
Business/IT alignment
Development
Visual Basic
Please help me complete this program! My task is to create a program to count the number of times the letter "a" occurs in a given string. the user will enter a string in a text box and the number of occurances of "a" will be displayed when the user clicks a button. I have already done the form and the user inputs a string into a text box named "txtinput" the answer is then displayed in a label named "lblOutput" I just need to know the code associated with the click event of the calculate button called "btnCalculate" Please email me the solutions at gjanderson16@hotmail.com thank you.
ASKED: January 13, 2007  7:12 AM
UPDATED: January 15, 2007  10:17 AM

Answer Wiki

Thanks. We'll let you know when a new response is added.

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

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • Satyam79
    Public Class Form1 Dim SearchString As String Dim SearchChar As String = "a" Dim TestPos As Integer = 0 Dim lcnt As Integer Dim acnt As Integer Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click SearchString = TextBox1.Text lcnt = SearchString.Length() acnt = 0 TestPos = 0 For i As Integer = 1 To lcnt TestPos = InStr(i, SearchString, SearchChar, CompareMethod.Text) If TestPos > 0 Then acnt = acnt + 1 i = TestPos End If Next i Label1.Text = acnt.ToString() End Sub End Class
    0 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following