How do I count characters using substing and a do while loop?
Im trying to make a pogram that will allow the user to type in a phrase and when they click on the search button an inputbox will appear asking for what character to search for. The user types the character in and then a messagebox should tell him how many times the character appears in the phrase. I'm having trouble counting the characters. I have to use substring and do while.

  Dim keyword As Char
        Dim intCounter As Integer = 0
        Dim strUserInput As String = ""
        Dim txtlength As Integer
        strUserInput = CStr(txtSearch.Text)
        txtlength = strUserInput.Length()
        txtlength = AscW(keyword)
        keyword = CChar(InputBox("What charater are you looking for?", "Search"))
        Do While txtlength > intCounter

            txtSearch.Text.Substring(0, 1)
            MessageBox.Show("the character " & keyword & " appears " & intCounter & " times")
            intCounter += 1


        Loop


    End Sub

Your text could be considered as an array, Just walk through the array with a for loop and comare each char with the lookup char.

That's just it, my professo wants us to use substring and do while loop.

Loop through the string to get every char with Substring(counter, 1)
Counter goes from zero to stringlength-1.

This is what I came up with instead. This works perfectly.

    Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        'Declaring variables 
        Dim character As Char
        Dim intCounter As Integer = 0
        Dim strUserInput As String = ""
        Dim lengthofTxt, totalChar As Integer
        lengthofTxt = CInt(txtSearch.Text.Length)
        Try
            strUserInput = ((InputBox("What charater are you looking for?", "Search")))
            'If the user does not enter a character or enters more than one character, a messagebox will appear.
            'If the enters a wrong character then a messagebox will appear saying no match found, if the user does
            'enter the right character a messagebox will appear saying the amount of times that character appears.
            If CBool(CInt(strUserInput.Length > 1) Or CInt(strUserInput.Length = 0)) Then
                MessageBox.Show("Please enter one character.")

            Else : character = CChar((strUserInput))
                Do Until lengthofTxt = intCounter
                    If txtSearch.Text.Substring(intCounter, 1) = character Then
                        totalChar += 1
                    End If
                    intCounter += 1
                Loop
                If totalChar <> 0 Then
                    MessageBox.Show("The character " & character & " was found " & totalChar & " times.")

                Else

                    MessageBox.Show("There was no match found.")
                End If
            End If
        Catch e1 As OverflowException
            MessageBox.Show("Number too large.")
        Catch e2 As Exception
            MessageBox.Show("An error has occured" & ControlChars.Cr & " please try again")
        End Try

    End Sub
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.