Easy 0 solved 0% acceptance

Count Vowels

Count the vowels in a piece of text.

What you'll learn

  • Loop over the characters of a string
  • Test membership in a set of vowels
  • Ignore letter case

Problem

Count how many characters in text are vowels: a, e, i, o, u in either uppercase or lowercase.

Implement count_vowels(text).

Input

text: The text to inspect (ASCII characters).

Output

Return the number of vowels.

Constraints

0 ≤ length ≤ 10,000

Examples

Example 1
Input:
text = "Hello World"

Expected output:
expected = 3
Input:
std::string text = "Hello World";

Expected output:
long long expected = 3;
Input:
string text = "Hello World";

Expected output:
long expected = 3;

Explanation

e, o and o are vowels: 3.

Example 2
Input:
text = "AEIOU and y"

Expected output:
expected = 6
Input:
std::string text = "AEIOU and y";

Expected output:
long long expected = 6;
Input:
string text = "AEIOU and y";

Expected output:
long expected = 6;

Explanation

A, E, I, O, U, a are vowels; y is not counted: 6.

How to approach it

Try on your own first, then open one hint at a time.

Hint 1

Scan the text once and test each character against the vowel set.

Hint 2

Treat uppercase and lowercase vowels as the same kind of letter.

Hint 3

An empty string should simply return zero.

Expected complexity

O(n) time, O(1) extra space

References

This challenge is adapted for CodeUtility. Follow the statement and constraints above.

Learn from others

Community solutions

No published solutions yet. Solve this challenge to share the first one.