I am trying to write a proram that will search for character or letter and count how many times that character appears. The user will enter a phrase in a text box then press the search button, then an Inputbox will appear asking the user to enter a character. then a messagebox.show should appear giving the user the number of times the character appears. This is what I have so far. I am getting an error after i type the character i want to look for. Any help would be appreciated. Thanks.

    Private Sub btnSearch_Click(sender As System.Object, e As System.EventArgs) Handles btnSearch.Click
        Dim keyword As Char
        Dim txtlength As Integer
        Dim intCounter As Integer = 0


        txtlength = txtSearch.Text.Length()
        keyword = CChar(InputBox("What charater are you looking for?", "Search"))

        Do Until txtlength = intCounter

            If CBool(txtSearch.Text.Substring(CInt(intCounter), 1)) Then
                MessageBox.Show("The character appears " & intCounter & " times.")
                intCounter += 1

            End If

        Loop

  end sub

Quoted Text Here

your error probably comes from using the length as the limit for the Substring index. Also you're trying to convert a string to a boolean. And lastly the index of substring only changes when intcounter does. so as soon as a character doesn't match the keyword you hit an infinite loop, because intcounter can't change. A closed ended loop would work better here something like this:

Private Sub btnSearch_Click(sender As System.Object, e As System.EventArgs) Handles btnSearch.Click
    Dim keyword As Char
    Dim txtlength As Integer
    Dim intCounter As Integer = 0
    txtlength = txtSearch.Text.Length()
    keyword = InputBox("What charater are you looking for?", "Search")(0)
    For Each c As Char In txtSearch.Text
        If c.ToString.ToLower() = keyword.ToString.ToLower() Then
            intCounter += 1
        End If
    Next
    MessageBox.Show("The character appears " & intCounter & " times.")
End Sub

A simpler way is to use LINQ, something like this:

Private Sub btnSearch_Click(sender As System.Object, e As System.EventArgs) Handles btnSearch.Click
    Dim keyword As Char
    Dim txtlength As Integer
    Dim intCounter As Integer = 0
    txtlength = txtSearch.Text.Length()
    keyword = InputBox("What charater are you looking for?", "Search")(0)
    intCounter = (From c In txtSearch.Text
                  Where c = keyword
                  Select c).ToList.Count
    MessageBox.Show("The character appears " & intCounter.ToString & " times.")
End Sub    

Thank you for the advice! How can I use substring to get the character that I want?

Substring isn't really necessary, since all strings are really character arrays and each character in the string can be accessed by its index. Here's another way using a For loop:

    For I = 0 To txtlength - 1
        'If txtSearch.Text.Substring(I, 1)(0) = keyword Then
        If txtSearch.Text(I) = keyword Then
            intCounter += 1
        End If
    Next

Use the commented line if you must use the Substring method.

An easier way is with regular expressions.

Dim rex As New System.Text.RegularExpressions.Regex("")
Dim s As String = "It was the best of times. It was the worst of times."

MsgBox(rex.Matches(s, "s").Count)

which counts "s" or

Dim rex As New System.Text.RegularExpressions.Regex("")
Dim s As String = "It was the best of times. It was the worst of times."

MsgBox(rex.Matches(s, "s", rex.Options.IgnoreCase).Count)

which counts "s" as well as "S"

Thank you so much. My professor wants us to write this program only using the substring method and loops to get the characters. Thank you. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.