C# How To Count Vowels in a String
Count Vowels
Loop through the characters of a string, and count the vowels (a, e, i, o and u):
Example
string text = "Hello World";
int count = 0;
foreach (char c in text.ToLower())
{
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')
{
count++;
}
}
Console.WriteLine("Number of vowels: " + count);
Explanation:ToLower() makes all letters lowercase, so "A" and "a" are both counted.
The foreach loop goes through each character c in the string. "Hello World" has 3 vowels: e, o and o.