Count vowels in a string
Count the English vowels in a string without treating uppercase and lowercase letters differently.
Course progress
Problem 13 of 100
Example → approach → runnable solution
Worked example
See the problem in action
Trace a concrete input through to the verified result.
Example input
text = "Build useful tools"
Expected output
7
What is happening?
The vowels are u, i, u, e, u, o, and o. Counting those seven characters gives the result.
Solution strategy
Build the right mental model
Normalize each character to lowercase and check whether it appears in the fixed set of vowels.
Core concepts
strings · character matching
Complexity
Time O(n), space O(1).
Runnable solution
Learn from working code
Compare languages, copy the code, or open it in the compiler.
Complete C# solution for count-vowels
using System;
class Program
{
static int CountVowels(string text)
{
const string vowels = "aeiou";
int count = 0;
foreach (char symbol in text)
if (vowels.IndexOf(char.ToLowerInvariant(symbol)) >= 0) count++;
return count;
}
static void Main()
{
Console.WriteLine(CountVowels("Build useful tools"));
}
}Knowledge check
Questions students often ask
What is the main idea behind count vowels in a string?
Normalize each character to lowercase and check whether it appears in the fixed set of vowels.
What is the time and space complexity of this solution?
Time O(n), space O(1).
Do all language tabs solve the same problem?
Yes. Every program uses the same example and produces the verified output shown on this page. The syntax and a few language-specific data structures differ.