Results 1 to 11 of 11

Thread: Question on String

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2012
    Posts
    25

    Question on String

    I need help, I've got this porgram to count vowels in a string but how can I get it to count constants too?
    Code:
    Module Module1
    
        Sub Main()
            Dim vowel As String
            Dim count As Integer
            Dim str As String
            Dim index As Integer
            Dim cons As String
            vowel = "aeiou"
            Console.WriteLine("Enter a String")
            str = Console.ReadLine()
            For index = 1 To Len(str)
                If InStr(vowel, Mid(str, index, 1)) Then
                    count = count + 1
                End If
            Next
    
            Console.WriteLine("There are " & count)
        End Sub
    
    End Module

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Question on String

    try this:

    vb Code:
    1. Dim s As String = "a short sentence"
    2. Dim vowels As String = "aeiou"
    3. MsgBox(s.Count(Function(c) vowels.Contains(Char.ToLower(c)))) 'vowels
    4. MsgBox(s.Count(Function(c) Asc(Char.ToLower(c)) >= 97 AndAlso Asc(Char.ToLower(c)) <= 122 AndAlso Not vowels.Contains(Char.ToLower(c)))) 'consonants

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: Question on String

    Y not Y? That letter is such an odd duck.

    By the way, all of that code is VB6, not .NET. Is the question really about .NET?
    My usual boring signature: Nothing

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Question on String

    Quote Originally Posted by Shaggy Hiker View Post
    Y not Y?
    where does the Y come into it?

  5. #5
    Fanatic Member proneal's Avatar
    Join Date
    May 2011
    Posts
    762

    Re: Question on String

    MattP also had a neat way of getting both vowels and cons..
    vb Code:
    1. Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
    2.  
    3.         Dim testString As String = "Rather than writing out each of the consonants I made a method to check for the vowels and replace them with an empty string"
    4.         Dim vowels = ExtractVowels(testString, True)
    5.         Dim cons = ExtractConsontant(testString, True)
    6.  
    7.         Console.WriteLine("Counted " & vowels.Length & " vowels. They are " & vowels.Trim.Replace(" ", String.Empty)) ' vowels list
    8.  
    9.         Console.WriteLine("Counted " & cons.Length & " Cons. They are " & cons.Trim.Replace(" ", String.Empty)) ' cons list
    10.        
    11.     End Sub
    12.     Private Shared Function ExtractVowels(inputString As String, includeY As Boolean) As String
    13.         Dim pattern As String = If(includeY, "[^aeiouyAEIOUY$]", "[^aeiouAEIOU$]")
    14.         Return String.Join(Nothing, System.Text.RegularExpressions.Regex.Split(inputString, pattern))
    15.     End Function
    16.     Private Shared Function ExtractConsontant(inputString As String, includeY As Boolean) As String
    17.         Dim pattern As String = If(includeY, "[aeiouyAEIOUY]", "[aeiouAEIOU]")
    18.         Return System.Text.RegularExpressions.Regex.Replace(inputString, pattern, String.Empty).Replace(" ", String.Empty)
    19.     End Function

    from here

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: Question on String

    Y is often, though not always, considered a vowel.
    My usual boring signature: Nothing

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Question on String

    Quote Originally Posted by Shaggy Hiker View Post
    Y is often, though not always, considered a vowel.
    well that's something i've never encountered before... must be foreign, cos it's not english

    Quote Originally Posted by http://simple.wikipedia.org/wiki/Vowel

    There are only five letters used to write vowels in English. They stand for about 20 vowel sounds in most English accents,[1] so these letters are a source of ambiguity in pronunciation for learners.

    These letters are vowels:
    A, E, I, O, U

    The rest of the letters of the alphabet are consonants:
    B, C, D, F, G, H, J, K, L, M, N, P, Q, R, S, T, V, W, X, Y, and Z

    Y and W can be considered as (approximants).
    edit: the best i can agree on is that Y + W are Semivowels
    Last edited by .paul.; Apr 15th, 2012 at 07:53 PM.

  8. #8
    Fanatic Member proneal's Avatar
    Join Date
    May 2011
    Posts
    762

    Re: Question on String

    Sybil would argue she has 2 vowels in her name.
    I would say that she just needs to change her name to Alice, then She gains an 'ace' by picking up an extra vowel then she can see Y she is full of BS.

  9. #9
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: Question on String

    I'm not surprised that a Brit would complain about American english, there is plenty to complain about. However, the true nature of Y is a bit baffling. Sometimes it is a vowel, sometimes it isn't. I guess everybody gets to choose, but I would say that it is more a vowel than not.

    EDIT: After a quick search on Google: Y is a vowel by any definition. So Y not include it?
    Last edited by Shaggy Hiker; Apr 15th, 2012 at 09:57 PM.
    My usual boring signature: Nothing

  10. #10
    Fanatic Member proneal's Avatar
    Join Date
    May 2011
    Posts
    762

    Re: Question on String

    I was edumacated with Y as a vowel, sometimes..
    Let's just call it a bi-curious Y.
    I wonder of G is sometimes a vowel?
    Nevermind...

  11. #11
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: Question on String

    Yeah, let's not go there. I ended up doing more research into vowels than was worth it for this. I would say that Y should be included.
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width