Code Logo

Vowel Counter

Published atDate not available
Easy 0 views
Like0

This problem is like a tiny letter hunt. You are given a word or sentence, and your job is to count how many vowels appear inside it. The vowels are a, e, i, o, and u.

You do not need to change the text or rearrange anything. You simply look at each character and count the ones that are vowels. If a letter is not a vowel, you ignore it and move on. Uppercase letters can count too if the problem says so, so it is smart to be careful with both big and small letters.

For example, "apple" has 2 vowels because it contains a and e. The word "sky" has 0 vowels because none of its letters belong to the vowel group. A longer word like "education" has even more, so careful counting matters.

The answer is always just one number: how many vowels you found. If the text is empty, the answer is 0. If the same vowel appears many times, each one counts, so you have to scan the whole text from start to finish.

Example Input & Output

Example 1
Input
s = "apple"
Output
2
Explanation

Example 1: "apple" contains 2 vowels (a, e)

Example 2
Input
s = "sky"
Output
0
Explanation

Example 2: "sky" contains no vowels

Example 3
Input
s = "education"
Output
5
Explanation

Example 3: "education" contains 5 vowels (e, u, a, i, o)

Algorithm Flow

Recommendation Algorithm Flow for Vowel Counter
Recommendation Algorithm Flow for Vowel Counter

Best Answers

java
class Solution {
    public int count_vowels(String s) {
        int count = 0;
        String vowels = "aeiouAEIOU";
        for (char c : s.toCharArray()) {
            if (vowels.indexOf(c) != -1) count++;
        }
        return count;
    }
}