Skip to the content

Count the words

Problem

Write a function that takes a phrase and returns how many words it has. Words are separated by spaces and the extra spaces do not count:

" hello world " has 2 words. An empty phrase, or one with nothing but spaces, has 0.

Examples

  • "write some code every day" → 5

  • "" → 0

  • Extra spaces

    " hello world " → 2

Besides these, the challenge has hidden tests that are revealed when you submit your solution.

You start with this

Python

def count_words(phrase):
    pass

JavaScript

function countWords(phrase) {
}
Solve this challenge

It opens in your browser, with the editor and the tests. This challenge is part of the full plan; the O(1) and O(log n) ones are free.

More O(n) challenges

See all challenges →