Start learning free

LESSON 220 OF 285 · LOOPS WITH STRINGS, ARRAYS & DICTIONARIES

Count the Vowels in Go: range 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

    GO · LOOPS WITH STRINGS, ARRAYS & DICTIONARIES

    Count The Vowels in Go, explained

    Problem overview

    The function countVowels receives a string s and returns how many vowels it contains. 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 (consonants, spaces, digits, punctuation) is skipped.

    "Hello World" has 3 vowels: e, o and o. "AlgoCademy" has 4: A, o, a and e, and the upper-case A counts. "Why try?" has 0, because y doesn't count.

    Approach

    Look at every character once, and add 1 to a counter when the character is one of the ten vowel characters. Two Go tools make this short:

    • for _, c := range s visits each character of a string.
    • strings.ContainsRune(vowels, c) reports whether the character c appears anywhere in the string vowels.

    Putting both cases into one string, "aeiouAEIOU", avoids converting s to lower case first.

    Step by step

    1. Store the vowels in a variable named vowels, declared with :=: "aeiouAEIOU". There are ten characters, five lower and five upper, and no y.
    2. Create count with :=, starting at 0. No vowel has been found yet.
    3. Loop over the string with range, using _ for the index and c for the character. Write the closing brace right away.
    4. Test the character. If strings.ContainsRune(vowels, c) is true, add 1 to count with ++. Other characters are not in vowels, so they are skipped.
    5. Return count after the loop. Returning inside the loop would stop at the first character.

    The solution in Go

    func countVowels(s string) int {
        vowels := "aeiouAEIOU"
        count := 0
        for _, c := range s {
            if strings.ContainsRune(vowels, c) {
                count++
            }
        }
        return count
    }
    

    What to know about these Go details:

    • range over a string gives runes. Each c is a rune, which is an integer holding a Unicode code point, and not a one-character string. That is the type ContainsRune takes. A vowel such as 'e' is written with single quotes when you need a rune literal.
    • The index comes first. range yields an index and a value. Go refuses unused variables, so the index is discarded with _.
    • Indexes count bytes. For "héy", ranging gives the indexes 0, 1 and 3, because é takes two bytes in UTF-8, and the character values 104, 233 and 121. This is why range is better than s[i], which gives one byte at a time, when text can contain non-ASCII characters.
    • The condition has no parentheses. Go writes if conditions without them, and the braces are required.

    Complexity

    The loop visits each character once, and each ContainsRune call scans a string of ten characters, a fixed amount of work, so the time is proportional to the length of s. The space is constant.

    Edge cases and common mistakes

    • Upper-case vowels. Leaving out AEIOU makes "AlgoCademy" count 3 instead of 4.
    • The letter y. "Why try?" must return 0.
    • Non-letters. Spaces, digits and punctuation are not in vowels, so they never count.
    • Returning too early. A return inside the loop ends the function at the first character.
    • Using a string for c. Because c is a rune, strings.Contains(vowels, c) doesn't compile. Use ContainsRune, as the lesson does.

    Testing

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