It's pretty simple:
You have a string, you loop through each character of the string and increment the counter every time it encounters a vowel.
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
How about this (if your strings are relatively short):
To get a count of each vowel without 'walking the string':
' strMyText is the string returned from the console
dim strMyTextCaps = strMyText.toUpper() 'We don't care about case
dim intTextLen = strMyText.length ' So we don't reevaluate every time
dim intCountA = intTextLen - (strMyTextCaps.Replace("A","").length)
dim intCountE = intTextLen - (strMyTextCaps.Replace("E","").length)
' etc...
It should be fairly obvious, but what we're doing is getting the length of the original string, and then replacing each vowel with an empty string. We subtract the new length from the original length and know the number of characters replaced. This is the vowel count for that vowel.
Haven't benchmarked for performance (nor have I run this - I believe the syntax is correct). A single line for each vowel is easier to read than a bunch of loops. Performance should not be a problem anyway, if the text is provided via the console.
Happy Coding!
Ned
If you're replacing every vowel with the same "", how would you tell the difference between each vowel?
Wait unless you're reading the string five times in, i.e the number of vowels there are aeiou?
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
We don't care about the number of "",s we care about the difference between the old length (with the vowels intact) and the new length (with one of the vowels converted to ""). Example:
Text = "I am here"
Length of original Text = 9
Text after Replace("a","") = "I m here"
Length of new Text = 8
Difference = 1, therefore Count of A's in the text = 1.
We can ignore leading and trailing blanks because we are not counting any characters - we're replacing all occurances of one character with a no-space and getting the difference in lengths.
Yes I just realised that and have since edited my post, however, your program requires you to read the string in five times.
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
I agree with that! It's a nice simple solution and would be tolerable if the string is short, like you have originally said.
I've only just really begun GUI coding, I mainly focus on code that is written. I enjoy using OOP and all it's advantages (inheritance etc) I am also a fan of data structures, i.e using stacks binaray trees and queues.
Like you I have an affinity for all languages, and I also enjoy working with 3d models graphics and rendering.
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
I see this as a more reliable word count:
Sub BtnEnterClick(sender As Object, e As System.EventArgs)
Dim strText As String
strText = txtInput.Text
strText = " " & strText 'insert a leading space
Dim array As Char() = CType(strText, Char())
'converts string to a character array
Dim count As Integer
count = 0
Dim i As Integer
For i=0 To Len(strText)-2 Step 1
If array(i) = " " And array(i + 1) <> " " Then
count = count + 1
End If
Next
txtResults.Text = count
End Sub
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
The minus 2 is so that the array isn't searched beyound its physical dimensions.
Normally because you are not looping through the entire string, the string length -2 part would cause some other problem. However, it's ok in this particular case as you may well have discovered through testing.
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439