1,105,409 Community Members

VB.Net - no. of vowels using StringBuilder

Member Avatar
patra.pritam
Newbie Poster
1 post since Dec 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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
    
Member Avatar
tinstaafl
Postaholic
2,012 posts since Jun 2010
Reputation Points: 559 [?]
Q&As Helped to Solve: 402 [?]
Skill Endorsements: 35 [?]
 
0
 

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

Member Avatar
Reverend Jim
Noli mentula
5,404 posts since Aug 2010
Reputation Points: 746 [?]
Q&As Helped to Solve: 647 [?]
Skill Endorsements: 51 [?]
Moderator
Featured
 
0
 

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
Next

MsgBox("number of vowels = " & numVowels)
You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: