In the previous lesson, we handled the edge case of zero-length input - when there are no characters at all to process. But there's another edge case lurking in our word counting algorithm that we need to address.
What happens when our input contains only spaces? Let's think about it:
Input: " " (five spaces)
Expected word count: 0
There are no words here, just whitespace, but our current algorithm counts spaces and adds 1, so it would return 6 (five spaces + 1 = six words). That's clearly wrong!
Our current algorithm uses this formula:
wordCount = spaceCount + 1
This formula assumes there's at least one word in the input. It works for normal sentences like "Hello World" (1 space → 2 words), but it breaks down when the input is nothing but spaces.
We need to check if the input contains only spaces before we apply our formula. If every character is a space, the word count should be 0.
Here's the approach:
0 (no words)spaceCount + 1 (our normal formula)If spaceCount == len(data), that means every single character was a space. There are no non-space characters, therefore no words.
If spaceCount < len(data), there must be at least one non-space character, which means we have at least one word, and our formula works.
Modify your countWords function to handle the all-spaces edge case:
Remember: this is a temporary fix for our naive algorithm. In the next few lessons, we'll discover that this algorithm has even more fundamental problems that we'll need to solve with a better approach!