Start learning free

LESSON 221 OF 292 · LOOPS WITH STRINGS, ARRAYS & DICTIONARIES

Count the Vowels in Swift: Looping Over a String

Practice Read the full explanation

Given a string s, return how many vowels it has.

The vowels are a, e, i, o and u, in lower case or in upper case. The letter y is not a vowel here. Every other character (consonants, spaces, digits, punctuation) is skipped.

 

Example 1:

Input: s = "Hello World"
Output: 3
Explanation: The vowels are e, o and o. The other characters, the space included, are skipped.

Example 2:

Input: s = "AlgoCademy"
Output: 4
Explanation: A, o, a and e are vowels: the upper-case A counts too. The y doesn't count.

Example 3:

Input: s = "Why try?"
Output: 0
Explanation: y is not a vowel, so this string has none.

 

Constraints:

  • s has at least one character and doesn't start or end with a space.
  • s contains English letters, digits, spaces and punctuation.
STEP 1 OF 5
    Ctrl + Enter to check

    SWIFT · LOOPS WITH STRINGS, ARRAYS & DICTIONARIES

    Count The Vowels in Swift, explained

    Problem overview

    The function countVowels receives a string s and returns how many vowels it has. The vowels are a, e, i, o and u, in lower or upper case. The letter y is not a vowel here, and every other character, such as consonants, spaces, digits and punctuation, is skipped.

    In "Hello World" the vowels are e, o and o, so the answer is 3. In "AlgoCademy" they are A, o, a and e, which makes 4: the capital A counts and the y doesn't. In "Why try?" there are none, so the answer is 0.

    Approach

    Look at each character once and keep a count. For each character, ask one question: is it one of the ten vowel characters? A string holding all ten, "aeiouAEIOU", answers that question with its contains method. Putting both cases in the string means you don't need to convert anything to lower case.

    Step by step

    1. Store the vowels in let vowels = "aeiouAEIOU". It is a let because it never changes. The letter y is not in it.
    2. Create var count = 0 for the vowels found so far. It is a var because it grows inside the loop.
    3. Loop over s with for c in s. Looping over a String gives its elements one at a time, and each is a Character.
    4. Test each character. vowels.contains(c) is true when c appears in vowels. If it does, add 1 to count with += 1, since Swift has no ++.
    5. Return count after the loop, so every character is checked first.

    The solution in Swift

    class Solution {
        func countVowels(_ s: String) -> Int {
            let vowels = "aeiouAEIOU"
            var count = 0
            for c in s {
                if vowels.contains(c) {
                    count += 1
                }
            }
            return count
        }
    }
    

    Swift details worth knowing:

    • Strings hold characters. A String is a collection of Character values, and that is why for c in s works and why vowels.contains(c) accepts a Character. You can't get a character with s[0]: Swift strings aren't indexed by integers. Loop over them, or use Array(s) when you need positions.
    • count as a name. Strings and arrays also have a property named count. Here count is a local variable, so there is no clash, but s.count would be the number of characters in s.
    • Exact comparison. contains compares characters exactly, so "A" and "a" are different characters. That is why both cases are in vowels.
    • Single quotes don't exist. A one-character literal is written with double quotes, like "a", and Swift decides from the context whether it is a String or a Character.

    Edge cases and common mistakes

    • No vowels. "Why try?" and "rhythm" both give 0.
    • Upper case. "AEIOU" gives 5. A vowel string with only lower-case letters would miss them.
    • Digits and punctuation. "a1e2" gives 2, because the digits aren't in vowels.
    • Repeated vowels. "queue" gives 4: u, e, u and e each count, because the loop visits characters, not distinct letters.
    • Returning inside the loop. The function would stop after the first character.
    • Treating y as a vowel. The lesson says it isn't.

    Testing

    • "Hello World" gives 3.
    • "AlgoCademy" gives 4.
    • "Why try?" gives 0.
    • "queue" gives 4.
    • "AEIOU" gives 5.
    • "a1e2" gives 2.
    Keep going with Swift1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free