Results 1 to 5 of 5

Thread: question

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2002
    Posts
    60

    Unhappy question

    Can somebody tell me how I would get started counting the total number of "a"s in a text box as text is entered (realtime) ? Thanks.

  2. #2
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    Re: question

    Originally posted by andrew104
    Can somebody tell me how I would get started counting the total number of "a"s in a text box as text is entered (realtime) ? Thanks.
    I wrote a function, although inefficient to do this, I dont know a better way maybe someone can come up with a more efficient way, I'm too dumb to do that
    VB Code:
    1. ' Counts the number of times that character c is used in the string txt
    2.     Function CountChars(ByVal txt As String, ByVal c As Char) As Integer
    3.         Dim i As Integer
    4.         For i = 0 To txt.Length - 1
    5.             If txt.Substring(i, 1) = c Then
    6.                 CountChars += 1
    7.             End If
    8.         Next
    9.     End Function


    so you can call this function like:

    CountChars (textBox1.text, "a"c ) ' returns the number of times character a is used in textbox1

    you can call this function in textbox's Changed event if you want it to be "realtime"
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  3. #3

    Thread Starter
    Member
    Join Date
    Oct 2002
    Posts
    60

    Thanks - more questions!

    Thank you for the help. I have figured out the single character,
    however...

    1. I'm now having trouble with counting the instances of "st".
    I've tried an if statement (see code) and it counts correctly,
    however if "s" is not the last character, then it displays "0" in the
    label again. Any ideas?

    2. Also, with the functions that I'm using, is there an easier way
    to count lower and uppercase letters, rather than having
    separate functions for each?

    3. I need to count letters (A-Z). How can I keep a count of
    those?

    4. I also need to count words. Meaning a group of letters with a
    space before and after them. Any ideas?


    Thank you so much for your help!


    VB Code:
    1. ' Count the number of times that character a is used in the string InputText
    2.     Function strCountA1(ByVal InputText As String, ByVal a As Char) As Integer
    3.         Dim i As Integer
    4.         For i = 0 To InputText.Length - 1
    5.             If InputText.Substring(i, 1) = a Then
    6.                 strCountA1 = strCountA1 + 1
    7.             End If
    8.         Next
    9.     End Function
    10.  
    11.     ' Count the number of times that character A is used in the string InputText
    12.     Function strCountA2(ByVal InputText As String, ByVal a As Char) As Integer
    13.         Dim i As Integer
    14.         For i = 0 To InputText.Length - 1
    15.             If InputText.Substring(i, 1) = a Then
    16.                 strCountA2 = strCountA2 + 1
    17.             End If
    18.         Next
    19.     End Function
    20.  
    21.     ' Count the number of times that character e is used in the string InputText
    22.     Function strCountE1(ByVal InputText As String, ByVal e As Char) As Integer
    23.         Dim i As Integer
    24.         For i = 0 To InputText.Length - 1
    25.             If InputText.Substring(i, 1) = e Then
    26.                 strCountE1 = strCountE1 + 1
    27.             End If
    28.         Next
    29.     End Function
    30.  
    31.     ' Count the number of times that character E is used in the string InputText
    32.     Function strCountE2(ByVal InputText As String, ByVal e As Char) As Integer
    33.         Dim i As Integer
    34.         For i = 0 To InputText.Length - 1
    35.             If InputText.Substring(i, 1) = e Then
    36.                 strCountE2 = strCountE2 + 1
    37.             End If
    38.         Next
    39.     End Function
    40.  
    41.     ' Count the number of times that character s is used in the string InputText
    42.     Function strCountS1(ByVal InputText As String, ByVal s As Char) As Integer
    43.         Dim i As Integer
    44.         For i = 0 To InputText.Length - 1
    45.             If InputText.Substring(i, 1) = s Then
    46.                 strCountS1 = strCountS1 + 1
    47.             End If
    48.         Next
    49.     End Function
    50.  
    51.     ' Count the number of times that character S is used in the string InputText
    52.     Function strCountS2(ByVal InputText As String, ByVal s As Char) As Integer
    53.         Dim i As Integer
    54.         For i = 0 To InputText.Length - 1
    55.             If InputText.Substring(i, 1) = s Then
    56.                 strCountS2 = strCountS2 + 1
    57.             End If
    58.         Next
    59.     End Function
    60.  
    61.     ' Count the number of times that character v is used in the string InputText
    62.     Function strCountV1(ByVal InputText As String, ByVal s As Char) As Integer
    63.         Dim i As Integer
    64.         For i = 0 To InputText.Length - 1
    65.             If InputText.Substring(i, 1) = s Then
    66.                 strCountV1 = strCountV1 + 1
    67.             End If
    68.         Next
    69.     End Function
    70.  
    71.     ' Count the number of times that character V is used in the string InputText
    72.     Function strCountV2(ByVal InputText As String, ByVal s As Char) As Integer
    73.         Dim i As Integer
    74.         For i = 0 To InputText.Length - 1
    75.             If InputText.Substring(i, 1) = s Then
    76.                 strCountV2 = strCountV2 + 1
    77.             End If
    78.         Next
    79.     End Function
    80.  
    81.     ' Count the number of times that characters st are used in the string InputText
    82.     Function CountSTTotal(ByVal InputText As String, ByVal t As Char) As Integer
    83.         Dim i As Integer
    84.         If InputText.EndsWith("s") Or InputText.EndsWith("S") Then
    85.             For i = 0 To InputText.Length - 1
    86.                 If InputText.Substring(i, 1) = t Then
    87.                     CountSTTotal = CountSTTotal + 1
    88.                 End If
    89.             Next
    90.         End If
    91.     End Function
    92.  
    93.     'test countwords
    94.     Function CountWords()
    95.         Dim strWords() As String
    96.         Dim lCounter As Long
    97.         Dim lWordCount As Long
    98.         strWords = Split(Trim(InputTextBox.Text), " ")
    99.         For lCounter = 0 To UBound(strWords)
    100.             If strWords(lCounter) <> " " And strWords(lCounter) <> "" Then lWordCount = lWordCount + 1
    101.         Next lCounter
    102.     End Function
    103.  
    104.     Private Sub InputTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)  Handles InputTextBox.TextChanged
    105.         Collection1.Add(Me.InputTextBox)
    106.         Me.ClearButton.Enabled = True
    107.         Me.ACountLabel.Text = strCountA1(InputTextBox.Text, "a"c) + strCountA2(InputTextBox.Text, "A"c)
    108.         Me.ECountLabel.Text = strCountE1(InputTextBox.Text, "e"c) + strCountE2(InputTextBox.Text, "E"c)
    109.         Me.SCountLabel.Text = strCountS1(InputTextBox.Text, "s"c) + strCountS2(InputTextBox.Text, "S"c)
    110.         Me.VCountLabel.Text = strCountV1(InputTextBox.Text, "v"c) + strCountV2(InputTextBox.Text, "V"c)
    111.         Me.STCountLabel.Text = CountSTTotal(InputTextBox.Text, "t"c)
    112.         Me.LetterCountLabel.Text = intLetterCount
    113.     End Sub
    114.  
    115.     'test count letters
    116.     Private Sub InputTextBox_KeyPress(ByVal KeyAscii As Integer)
    117.         If (KeyAscii > 64 And KeyAscii < 91) Or (KeyAscii > 96 And KeyAscii < 123) Then
    118.             'user typed a letter
    119.             intLetterCount = intLetterCount + 1
    120.             Me.LetterCountLabel.Text = intLetterCount
    121.         End If
    122.     End Sub

  4. #4

    Thread Starter
    Member
    Join Date
    Oct 2002
    Posts
    60

    Unhappy counting characters

    Anybody???

  5. #5
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    aah sorry that I'm not answering your question, I didnt even read what you asked (dont have vb right now... )

    but you're doing something wrong

    when I said it "counts the number of times character c is used," I ment that c as a variable, and not "c"
    You dont need to make many different functions to count different characters. Just use the ONE function that I wrote. You can use it like this:


    look for character "a" -> CountChars (textBox1.text, "a"c )
    look for character "b" -> CountChars (textBox1.text, "b"c )
    look for character "c" -> CountChars (textBox1.text, "c"c )
    look for character "d" -> CountChars (textBox1.text, "d"c )
    look for character "e" -> CountChars (textBox1.text, "e"c )


    one function is enought, makes sense?

    when I use something like "a"c, the c means that VB should threat "a" as a character and not as string. I dont know any other way to do this, I'm dumb maybe it's the only way to do it. In C++ you can put characters in ' (like 'a' , instead of "a"c )
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width