none
How do you create a Vowel Count application in Microsoft Visual Basic 2008 Express Edition? RRS feed

  • Question

  • Hi, I need to create an application that counts how many vowels there are in a text box and then displays the amount of them in a label concatenated with the phrase "The number of vowels in *Entered word or phrase* is: " after I push a button. It needs to count all vowels (including the letter "y") regardless of whether they are uppercase or lowercase, it must use a loop function, and obviously since it has to count and display the number of vowels after I press a button it all has to be done in one private sub.
    Wednesday, May 9, 2018 5:17 PM

All replies

  • You only need a loop if this is homework.

    Dim Input As String = "How can I count vowels in string by using loop and if,, I wrote this code which is not working don't know where I went wrong? I don't want to use array either"
    Console.WriteLine(New Regex("(?i)[aeiou]").Matches(Input).Count)
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, May 9, 2018 5:49 PM
    Moderator
  • Something like :

    Label1.Text = "The number of vowels in *" + TextBox1.Text + "* Is " + New RegularExpressions.Regex("(?i)[aeiouy]").Matches(TextBox1.Text).Count.ToString()


    • Edited by Castorix31 Wednesday, May 9, 2018 6:00 PM
    Wednesday, May 9, 2018 6:00 PM
  • First you create a Windows.Forms template application. Then you drag a Button, Label and TextBox onto it.

    You select the Form then the Button to establish code in the code window.

    After that you write code in the buttons click event sub to detect if the TextBox contains any text. If it does contain text you loop through the string verifying if each letter is a vowel based on some list of vowels or string of vowels maybe. Can't be that hard.

    Use ChrW(34) to add the double quote to a string.


    La vida loca

    Wednesday, May 9, 2018 6:02 PM
  • Use ChrW(34) to add the double quote to a string.


    La vida loca

    Or, ControlChars.Quote.

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Wednesday, May 9, 2018 7:14 PM
    Moderator
  • If you need loops, then check this sample:

       Dim number = 0

       For Each c In TextBox1.Text

          For Each v In "aeiouyAEIOUY"

             If c = v Then number += 1 : Exit For

          Next

       Next

       Label1.Text = "The number of vowels in """ & TextBox1.Text & """ is: " & number

     

    One more solution, without loops:

       Dim number = TextBox1.Text.Count(Function(c) "aeiouyAEIOUY".Contains(c))

     

    or:

       Dim number = TextBox1.Text.Count(AddressOf "aeiouyAEIOUY".Contains)

    Wednesday, May 9, 2018 8:51 PM
  • Hi Triple_s,

    One is use Hashset to do this:

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim total As Integer = 0
            Dim vowels = New HashSet(Of Char) From {"a"c, "e"c, "i"c, "o"c, "u"c, "y"c}
            Dim sentence As String = TextBox1.Text.ToLower()
            For i As Integer = 0 To sentence.Length - 1
                If vowels.Contains(sentence(i)) Then
                    total += 1
                End If
            Next
            Label1.Text = "The number of vowels in " & TextBox1.Text & "is  " & total
    
        End Sub

    Another is:

     Dim I As Integer
            Dim nVowels As Integer
            Dim Vowels As String
            Dim InputString As String
            Vowels = "aeiouy"
            InputString = TextBox1.Text
            For I = 1 To Len(InputString)
                If InStr(Vowels, Mid(InputString, I, 1).ToLower()) Then
                    nVowels = nVowels + 1
                End If
            Next
            Label1.Text = "The number of vowels in " & TextBox1.Text & "is " & nVowels

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, May 10, 2018 5:49 AM
    Moderator
  • Contributors:

    Please do not provide copy-paste solutions to questions which are almost certainly homework for a programming course.

    That is the opposite of helpful to the entire community.

    How do you know this is almost certainly homework?

    1. The user as zero points
    2. The issue would never apply to a real-world programming need
    3. The requirements are given in a step-by-step manner
    4. The requirements include inefficient mechanisims

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Thursday, May 10, 2018 11:47 AM
    Moderator
  • @Reed,

    Suggestion: As clear as this is without any changes. Make it a sticky. 

     


    Success
    Cor

    Thursday, May 10, 2018 11:49 AM
  • @Reed,

    Suggestion: As clear as this is without any changes. Make it a sticky. 

     


    Success
    Cor


    I thought about it, but some folks complain about the number of sticky threads.  But I think you are right that we need another one to address homework.

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Thursday, May 10, 2018 11:51 AM
    Moderator