-
Apr 14th, 2012, 11:59 PM
#1
Thread Starter
Junior Member
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
-
Apr 15th, 2012, 12:09 AM
#2
Re: Question on String
try this:
vb Code:
Dim s As String = "a short sentence"
Dim vowels As String = "aeiou"
MsgBox(s.Count(Function(c) vowels.Contains(Char.ToLower(c)))) 'vowels
MsgBox(s.Count(Function(c) Asc(Char.ToLower(c)) >= 97 AndAlso Asc(Char.ToLower(c)) <= 122 AndAlso Not vowels.Contains(Char.ToLower(c)))) 'consonants
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Apr 15th, 2012, 05:17 PM
#3
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
-
Apr 15th, 2012, 05:46 PM
#4
Re: Question on String
Originally Posted by Shaggy Hiker
Y not Y?
where does the Y come into it?
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Apr 15th, 2012, 06:02 PM
#5
Re: Question on String
MattP also had a neat way of getting both vowels and cons..
vb Code:
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click 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" Dim vowels = ExtractVowels(testString, True) Dim cons = ExtractConsontant(testString, True) Console.WriteLine("Counted " & vowels.Length & " vowels. They are " & vowels.Trim.Replace(" ", String.Empty)) ' vowels list Console.WriteLine("Counted " & cons.Length & " Cons. They are " & cons.Trim.Replace(" ", String.Empty)) ' cons list End Sub Private Shared Function ExtractVowels(inputString As String, includeY As Boolean) As String Dim pattern As String = If(includeY, "[^aeiouyAEIOUY$]", "[^aeiouAEIOU$]") Return String.Join(Nothing, System.Text.RegularExpressions.Regex.Split(inputString, pattern)) End Function Private Shared Function ExtractConsontant(inputString As String, includeY As Boolean) As String Dim pattern As String = If(includeY, "[aeiouyAEIOUY]", "[aeiouAEIOU]") Return System.Text.RegularExpressions.Regex.Replace(inputString, pattern, String.Empty).Replace(" ", String.Empty) End Function
from here
-
Apr 15th, 2012, 06:56 PM
#6
Re: Question on String
Y is often, though not always, considered a vowel.
My usual boring signature: Nothing
-
Apr 15th, 2012, 07:45 PM
#7
Re: Question on String
Originally Posted by Shaggy Hiker
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
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.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Apr 15th, 2012, 08:27 PM
#8
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.
-
Apr 15th, 2012, 09:54 PM
#9
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
-
Apr 15th, 2012, 10:13 PM
#10
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...
-
Apr 16th, 2012, 11:37 AM
#11
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|