Program using the StringBuilder class that will accept string from user and will count the numbers of vowels in it.

Drag and Drop:

  1. A Button
  2. A Textbox
  3. A Label

        Public Class Stringbuilder
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
                Dim mystring As String = TextBox1.Text
                Dim intnumber As Integer = 0
                Dim chars As Char() = mystring.ToCharArray()
                Dim newString As System.Text.StringBuilder = New System.Text.StringBuilder()
                For Each c As Char In chars
                    Dim strpattern As String
                    strpattern = "[U,u,E,e,O,o,A,a,I,i]"
                    Dim strstore As String
                    strstore = c Like strpattern
                    If strstore = True Then
                        intnumber = intnumber + 1
                    End If
                Next c
                newString.Append("No. of vowels: " & intnumber)
                Label1.Text = newString.ToString()
            End Sub
        End Class

Without knowing what it's doing wrong, it's hard to say what errors you have. A couple of obvious things I noticed, strstore should be declared as Boolean not String, and strpattern should be declared as String not Boolean. In fact for efficiency you should probably try to avoid declaring variables inside a loop. The loop has the same scope as the sub routine. Declaring the variables inside the loop means a lot of extra work for your program, because it will declare the variables each time on each cycle of the loop. And finally, a string like strpattern that won't change can be declared as a Const

If you are only interested in the total number of vowels and not the number of each vowel then a simpler approach is

Dim str As String = "the quick brown fox jumped over the lazy dog"
Dim numVowels As Integer = 0

For Each ch As Char In str.ToUpper.ToCharArray
    If InStr("AEIOUY", ch) > 0 Then numVowels += 1

MsgBox("number of vowels = " & numVowels)
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.