DSA Factory
Free lessonsStrings · Stage 0 · Walking a string · Step 1

A string is a row of characters

Visit each character of a string and count the vowels. About 6 minutes.

A string is a row of letters

Every message you type, every name in your contacts, is a string: a row of characters, one per box, numbered from 0 just like an array. "cat" has 3 boxes: c in box 0, a in box 1, t in box 2.

A space is a character too, and it takes a box. So "hi there" has 8 characters, not 7. Punctuation and digits count the same way.

One character per box: numbered from 0.
Spaces count: they take a box like any letter.
s = "hello world" (␣ is the space)
h
0
e
1
l
2
l
3
o
4
␣
5
w
6
o
7
r
8
l
9
d
10
i
count
0

Box 0 holds 'h'. Not a vowel. Just like an array, we look at one box at a time.

Move 1 of 6

Everything from arrays still works

Walking a string is walking an array: visit each box from the first to the last, and do something with the character you find. Counting vowels is the counting loop you already know, with a new question at each box: is this character one of a, e, i, o, u?

An empty string has no boxes, so the loop doesn't run and the count stays 0.

Same loop: a new question at each box.
In code
count = 0
for ch in s:
    if ch in "aeiou":
        count += 1
return count
Quick check

How many characters does "hi there" have?

  1. A8
  2. B7
  3. C2
Show the answer

8. h, i, space, t, h, e, r, e: the space counts as a character.

Your problem

Count the vowels

Return how many vowels are in s. The vowels are a, e, i, o and u. s has only lowercase letters and spaces.

Example
s = "hello world" → 3

0 ≤ length of s ≤ 1,000

Solve it in your browserPython, C++, Java or JavaScript. Hints if you get stuck. No sign-up needed.
Start coding