Click here to Skip to main content
15,886,740 members
Please Sign up or sign in to vote.
1.67/5 (4 votes)
See more:
Afternoon, I have searched for a way to count the vowels in a string and have got close,
VB
Sub countVowels(userInput As String, ByRef numberOfVowels As Double)

        Dim userInputArray() As Char
        Dim mycount As Integer
        Dim s As String

        userInputArray = userInput.ToCharArray

        System.Console.WriteLine("  THE USER ENTERED : [ " & userInput & " ] ")
        ' COMMENT : user the user input array variable to count the number of vowels

        numberOfVowels = 0
        If 
            (userInputArray(mycount) = "A") Or
            (userInputArray(mycount) = "E") Or
            (userInputArray(mycount) = "I") Or
            (userInputArray(mycount) = "O") Or
            (userInputArray(mycount) = "U") Or
            (userInputArray(mycount) = "a") Or
            (userInputArray(mycount) = "e") Or
            (userInputArray(mycount) = "i") Or
            (userInputArray(mycount) = "o") Or
            (userInputArray(mycount) = "u") Then




            numberOfVowels = numberOfVowels + 1

        End If

            Console.WriteLine("number of vowels:" & numberOfVowels)

    End Sub

[Edit: MTHeffron -- Added tags for formatting]
Posted
Updated 15-Dec-15 8:31am
v2
Comments
jgakenhe 15-Dec-15 14:14pm    
This should help out: http://www.freevbcode.com/ShowCode.asp?ID=605

You're getting close, but you need a loop to go through each of the characters in turn: at the moment you only look at the first one (and that by luck, since you don't ever assign a value to mycount).
I'd also use a Select Case instead of long If condition:
VB.NET
Dim vowels As Integer = 0
For Each c As Char In userInput
	Select Case c.ToLower()
		Case "a"C, "e"C, "i"C, "o"C, "u"C
			vowels += 1
			Exit Select
	End Select
Next
 
Share this answer
 
This code is what I call "anti-programming"; instead of using programming abstraction, it reflects the thinking of a non-engineer who tries to compare and count everything one by one. In "real" programming, the code is always much more expressive and shorter. Besides, it should be maintainable: you should avoid all your immediate constants defined implicitly and disperse over some range of code text. Better define all constants explicitly and group them together.

So, here is the simple algorithm for you:

First, define vowels. Usually, there is no predefined functions to tell vowels from consonants in all writing systems and languages, but as I can see, you are only interested in English. Correct programming style will help you to localize the algorithm to different culture. So, define the set of vowels as just one constant string. Don't put both lower-case and upper-case letters. Instead, do all calculations in either lower case or upper case. Let's assume, for all considerations below, you want to use lower-case. So, defined the string like "aeiouy".

Write a separate function NumberOfVowels(string). Always abstract out all sound functional units of your code into functions.

In this function, first calculate lower-case representation of your input string. It would be one call. Alternatively, do it per character inside the loop.

Now, the loop. Before the loop, declare the integer variable count and assign it to 0. Traverse all characters in the input value. For each character, perform the search of this character in the constant string of vowels ("aeiouy"). If the search is successful (ignore the index found; you only need to have it withing the constant string's length), increment count. The search is also only one call to the library function.

After the loop, return count.

That's all.

Now, you did not even specify what language do you mean by "Basic", exactly, but System.Console.WriteLine suggests it's VB.NET. Then the lower-case function could be System.String.ToLower() (or you can use System.String.ToUpper()), and the search function can be System.String.IndexOf(System.Char):
https://msdn.microsoft.com/en-us/library/system.string%28v=vs.110%29.aspx[^].

With the function System.String.IndexOf(System.String), "not found" condition will be indicated by return value less then zero.

[EDIT #1]

In comments to this answer, 0x01AA suggested a good alternative, LINQ. I like it, but I would find it quite difficult to explain it for you, because I don't know your background; understanding of LINQ could be pretty hard for a beginner. Perhaps 0x01AA will try to do it in his alternative answer.

[EDIT #2]

As Bruno (0x01AA) decided not to post an answer (see the comments below), I can explain how to get the LINQ solution, without writing any code.

The LINQ sample can be found here: https://msdn.microsoft.com/en-us/library/bb397940.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1[^].

How to change it to what you need? Simple. Replace the line
VB
Where Char.IsDigit(ch)

with
VB
Where vowels.Contains(ch.ToLower())

In this line, vowels should be your constant string of vowels you are searching in (equals to "aeiouy").

Sorry, but I cannot take the labor of explaining how LINQ works, what types are involved, and so on. I don't really know your background; and I guess you would need to learn a lot of fundamentals before you can really understand cunning LINQ techniques. Please see:
https://en.wikipedia.org/wiki/Linq[^],
https://msdn.microsoft.com/en-us/library/bb397926.aspx[^].

—SA
 
Share this answer
 
v7
Comments
[no name] 15-Dec-15 14:45pm    
I do not vote, but I think this time the "real" programming solution will use LINQ ;)

A 5, after googling this request.
Sergey Alexandrovich Kryukov 15-Dec-15 14:52pm    
Agree, not not sure it's the best for this inquirer, probably the beginner. I think main point is the idea to learn using programming, not "anti-programming" demonstrated in the inquirer's source code.

Perhaps it would be good to explain your LINQ solution is a separate answer, but I would suggest that you make it understandable for the inquirer; but understanding LINQ (not just using it in a blind-folded manner as many do) needs a lot of background.

Not a bad idea anyway, thank you.

—SA
[no name] 15-Dec-15 14:57pm    
Now the big Problem is: I heard about LINQ, I read about LINQ, but I'm far away to be able to give a serious answer using LINQ.

:) Bruno
Sergey Alexandrovich Kryukov 15-Dec-15 14:59pm    
Well, then let me note: event though I do not disagree with your opinion, and think your idea itself is good, but it should better be more argumentative. :-)
—SA
Sergey Alexandrovich Kryukov 15-Dec-15 14:56pm    
Anyway, I just credited your for this suggestion in my updated answer, after [EDIT].
I would be interested to know if you take the challenge of explaining it to the inquirer. :-)
Thank you very much.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900