We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,539 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

VB.Net - no. of vowels using StringBuilder

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
    
3
Contributors
2
Replies
2 Hours
Discussion Span
4 Months Ago
Last Updated
3
Views
patra.pritam
Newbie Poster
1 post since Dec 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 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

tinstaafl
Nearly a Posting Virtuoso
1,289 posts since Jun 2010
Reputation Points: 341
Solved Threads: 223
Skill Endorsements: 14

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)
Reverend Jim
Carpe per diem
Moderator
3,584 posts since Aug 2010
Reputation Points: 561
Solved Threads: 445
Skill Endorsements: 32

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0609 seconds using 2.67MB