Editor

Word Count

#208 · Strings · Beginner · 25 XP · about 3 min

Write word_count(text) that returns how many words text contains. A word is any run of characters that are not whitespace: words can be separated by any number of spaces, tabs (\t) or new lines (\n), and punctuation counts as part of the word it touches.

An empty string, or one with only whitespace in it, has 0 words.

Examples

word_count("The cat sat on the mat")  # returns 6
word_count("  lots   of   space  ")   # returns 3
word_count("well-known? It's 42!")    # returns 3

Fastest solvers

Leaderboard

First solves only, ranked by stars and then time.