DSA FUNDAMENTALS › STRING BASICS
A word is a maximal consecutive run of non-space characters. In the string ' code is fun ', the three words are 'code', 'is', and 'fun'. Each word ends when a space appears or when the string ends. The string has eight spaces, but those spaces are separators, not words, so they do not imply nine words.
Leading spaces occur before 'code', repeated spaces occur between 'code' and 'is', and trailing spaces occur after 'fun'. None of these spaces contains a non-space run. The word count must therefore come from the three runs of letters, giving a count of 3.
What is the word count of ' code is fun ', and why do its leading, repeated, and trailing spaces not create words?
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
You can count a word exactly once when you reach its first character. A character starts a word when it is not a space and either it is at index 0 or the character immediately before it is a space. This rule identifies index 2, where 'code' starts, index 9, where 'is' starts, and index 12, where 'fun' starts.
At index 3, the character is part of the existing 'code' run, so it is not another start. The same is true at indices 4, 5, 10, 13, and 14. Space characters also fail the first part of the test. Counting only the three successful positions counts each word once.
For every position i, the safe condition checks that the current character is not a space, then checks whether i is zero or the previous character is a space. The index-zero check must come before s[i - 1]. When i is 0, the first part of the or expression is true, so short-circuit evaluation skips the second part and never accesses index -1.
if (s[i] != ' ' && (i == 0 || s[i - 1] == ' ')) {
count++;
}if (s.charAt(i) != ' ' && (i == 0 || s.charAt(i - 1) == ' ')) {
count++;
}Complete the condition so it detects a word start safely, with the index-zero test before the previous-character access.
if (s[i] != ' ' && (???)) count++;Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
Start the counter at 0 and inspect the string from index 0 through index 16. The counter stays 0 through the leading spaces, becomes 1 at index 2, stays 1 through the rest of 'code' and the repeated spaces, becomes 2 at index 9, and becomes 3 at index 12. It never increments at any other index.
| INDEX RANGE | WHAT THE SCAN SEES | COUNTER AFTER THE RANGE |
|---|---|---|
| 0-1 | Leading spaces | 0 |
| 2 | Start of 'code' | 1 |
| 3-8 | Rest of 'code' and repeated spaces | 1 |
| 9 | Start of 'is' | 2 |
| 10-11 | Rest of 'is' and one space | 2 |
| 12 | Start of 'fun' | 3 |
| 13-16 | Rest of 'fun' and trailing spaces | 3 |
count = 0
index 0: space, count = 0
index 1: space, count = 0
index 2: word start, count = 1
indices 3-8: no word start, count = 1
index 9: word start, count = 2
indices 10-11: no word start, count = 2
index 12: word start, count = 3
indices 13-16: no word start, count = 3The scan visits each of the 17 characters once, so its time complexity is O(n), where n is the string length. It stores only the counter and the current index, so it uses O(1) extra space. The string stays unchanged because counting needs only character reads and does not need to remove or rearrange any spaces.