AlgoMaster Logo

Count Words in a Sentence

Last Updated: June 7, 2026

easy
3 min read

Understanding the Problem

The spacing is what makes this nontrivial. A clean sentence like "hello world" has two words, but real input might start with spaces, end with spaces, or pack several spaces between words.

Consider " leading and trailing ". The extra spaces at the front and back should not add phantom words. The answer is still 3, for "leading", "and", and "trailing".

Define a word as a contiguous run of non-space characters. The task is then to count how many such runs appear, no matter how the spaces are arranged around them. Two methods follow. The first counts the points where a word begins. The second splits the string into pieces and counts the non-empty ones.

Key Constraints:

  • A word is a run of non-space characters → The count depends on the groups of non-space characters, not on the number of spaces. The string "multiple spaces here" has three such groups, so the answer is 3.
  • Extra spaces must not create empty words → Leading spaces, trailing spaces, and repeated spaces all need to be handled so they never inflate the count.
  • An empty string and an all-space string return 0 → Neither contains a single non-space character, so there are no words to count.

Approach 1: Transition Counting

Intuition

Every word begins exactly once, when a non-space character follows a space or the start of the string. Counting these starting points counts the words.

Walk through the string. A new word starts when the current character is not a space and the character just before it either does not exist or is a space. Counting these space-to-non-space transitions gives the word count and naturally ignores leading, trailing, and repeated spaces.

Algorithm

  1. Initialize count to 0.
  2. Walk through the string by index, from the first character to the last.
  3. For the current character, check whether it is a non-space character.
  4. If it is non-space and either it is the first character or the previous character is a space, a new word has started, so add 1 to count.
  5. After the scan, return count.

Example Walkthrough

Input:

0
1
a
2
3
4
b
5
b
s

Start with count = 0. Index 0 is a space, so nothing happens. Index 1 is a, which is non-space and follows a space, so a word starts and count becomes 1. Indexes 2 and 3 are spaces, so nothing happens. Index 4 is b, which is non-space and follows a space, so count becomes 2. Index 5 is b, non-space but following another non-space, so no new word starts. After the scan, the answer is 2.

2
result

Code

The transition scan counts words in place without building anything extra. A different way leans on splitting the string and counting the pieces that survive.

Approach 2: Split on Whitespace

Intuition

Most languages can split a string into pieces wherever whitespace appears, and each piece is a candidate word. The complication is that leading, trailing, or repeated spaces produce empty pieces, so counting every piece overcounts.

Trim the surrounding spaces and split on one or more spaces to leave only the real words. Counting the resulting non-empty pieces gives the word count. This trades a bit of extra memory for code that reads closer to the problem description.

Algorithm

  1. Remove the spaces at the start and end of the string.
  2. If the trimmed string is empty, return 0 because there are no words.
  3. Split the trimmed string into pieces wherever one or more spaces appear.
  4. Return the number of pieces.

Example Walkthrough

Input:

0
1
2
a
3
4
5
b
6
b
7
8
s

Trimming " a bb " removes the leading and trailing spaces, leaving "a bb". Splitting that on runs of spaces produces the two pieces "a" and "bb". Both pieces are non-empty, so the answer is 2.

2
result

Code