Practice

Count Words in a Sentence

MEDIUMString Processing7 marks

Read a sentence and output its number of words.

Words are groups of non-space characters. The sentence may contain leading, trailing, or repeated spaces.

Input: One string. Output: The number of words.

Example:

Input:  pseudocode is useful
Output: 3

Sample Test Cases

Test 1: Three words
Inputs: pseudocode is useful
Expected: 3
Test 2: One word
Inputs: Hello
Expected: 1

Premium Question

This question needs a paid plan — upgrade, or join a class from a teacher who has one. You can read the description and sample tests, but grading is locked.

Sign in to get started