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
Where Char.IsDigit(ch)
with
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