Course outline
Course / The terminal and the language

Count the vowels

Lab

How to fetch and submit this lab

Once: install the tool and connect your account.

curl -fsSL https://gonomicon.com/install.sh | sh
gonomicon login

Each 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 submit

Go installed? gonomicon test runs the visible test on your computer first. Submitting also runs hidden checks on our server.

Goal 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. CountVowels has a // TODO comment and a return line you will change.
  • Use range Text to visit each character; you don't need the index, so ignore it with _.
  • Count a character if it's a, e, i, o, or u, in either lowercase or uppercase.
  • Declare the counter before the loop, starting at 0.
  • Text can be empty, in which case the loop never runs and the count stays 0.
  • Do not change var Text string or the CountVowels function 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

  1. Range over Text to get each rune in turn, and check whether it's one of the ten vowel letters (five lowercase, five uppercase).
  2. A switch with one case listing all ten letters together is a clean way to check membership — increment a counter inside that case.
  3. count := 0 for _, r := range Text { switch r { case 'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U': count++ } } return count

Something wrong with this lesson? Report a problem