DSA SheetLesson · no judge

DSA FUNDAMENTALSSTRING BASICS

Count Words by Detecting Their Starts

Reading · 6 minQuiz · 5 questions2 code drills · run onlyGenerated by gpt-5.6-luna · Aug 26

Words are runs of non-space characters, not spaces plus one

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.

CHECKPOINT 1Not answered

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.

Every word can be counted at its first character

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.

the indexed string ' code is fun ' with its three word startsThe string has 17 indexed cells. Indices 0 and 1 are spaces, indices 2-5 contain 'code', indices 6-8 are spaces, indices 9-10 contain 'is', index 11 is a space, indices 12-14 contain 'fun', and indices 15-16 are spaces. Word-start markers appear only at index 2 under 'c', index 9 under 'i', and index 12 under 'f'.··code···is·fun··012345678910111213141516codeisfun● word start · spaceword starts: indices 2, 9, 12
A word contributes one count at the first character of its non-space run.

The index-zero test must happen before reading the previous character

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.

CPPC++ safely checks index 0 before reading the previous character.
if (s[i] != ' ' && (i == 0 || s[i - 1] == ' ')) {
    count++;
}
JAVAJava uses the same short-circuit rule with charAt.
if (s.charAt(i) != ' ' && (i == 0 || s.charAt(i - 1) == ' ')) {
    count++;
}
CHECKPOINT 2Not answered

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.

One left-to-right scan counts all three words without changing the string

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 RANGEWHAT THE SCAN SEESCOUNTER AFTER THE RANGE
0-1Leading spaces0
2Start of 'code'1
3-8Rest of 'code' and repeated spaces1
9Start of 'is'2
10-11Rest of 'is' and one space2
12Start of 'fun'3
13-16Rest of 'fun' and trailing spaces3
Counter changes at the three word starts.
TEXTThe counter changes only when the word-start test succeeds.
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 = 3

The 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.