Course / The terminal and the language
Count the vowels
How to fetch and submit this lab
Once: install the tool and connect your account.
curl -fsSL https://gonomicon.com/install.sh | sh
gonomicon loginEach lab: download it, edit the file named in the instructions, then submit.
gonomicon fetch week-1/11-02-count-the-vowels
cd 11-02-count-the-vowels
gonomicon submitGoal
Count how many vowels (a, e, i, o, u, either case) appear in Text, using range over the string.
Example
If you needed to count only the letter 'o' in a string, you'd write:
count := 0
for _, r := range Text {
if r == 'o' || r == 'O' {
count++
}
}
Rules
- Edit
vowels.go.CountVowelshas a// TODOcomment and a return line you will change. - Use
range Textto visit each character; you don't need the index, so ignore it with_. - Count a character if it's
a,e,i,o, oru, in either lowercase or uppercase. - Declare the counter before the loop, starting at
0. Textcan be empty, in which case the loop never runs and the count stays0.- Do not change
var Text stringor theCountVowelsfunction signature. Do not add imports.
What you'll practice Ranging over a string to visit each character, and building up a count with a counter declared outside the loop.
Run it
gonomicon fetch week-1/11-02-count-the-vowels
cd 11-02-count-the-vowels
gonomicon test
gonomicon submit
Hints
- Range over Text to get each rune in turn, and check whether it's one of the ten vowel letters (five lowercase, five uppercase).
- A switch with one case listing all ten letters together is a clean way to check membership — increment a counter inside that case.
- count := 0 for _, r := range Text { switch r { case 'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U': count++ } } return count