Opening the reading…
Opening the reading…
DSA FUNDAMENTALS › STRING BASICS
The string " DSA is fun " has spaces in three different roles. The two spaces before D are leading spaces, the two spaces after n are trailing spaces, and the runs between DSA, is, and fun are internal spaces. The required result is "DSA is fun": leading and trailing spaces disappear, while each internal run becomes exactly one space.
The original string has length 17, but the normalized string has length 10. You must preserve every non-space character and its order. You must also decide whether a space is internal before copying it, because a space at the end should not reach the result.
What is the exact required output for the string " DSA is fun "?
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
Use a result string and a flag named pendingSpace. Scan the input from left to right. When you see a non-space character, copy it to result. When you see a space after result already contains a character, set pendingSpace to true but do not append a space yet. A leading space cannot set the flag because result is still empty.
For " DSA is fun ", the first two spaces are ignored. D, S, and A are copied directly, so result becomes "DSA". The three spaces at indices 5-7 set pendingSpace to true. The spaces do not create three output characters, and the flag remains true while that internal space run continues.
The next non-space character decides whether a pending space was truly internal. If the current character is a space, set pendingSpace to true only when result is not empty. If the current character is not a space, append one space first when pendingSpace is true, then append the character and reset the flag.
for each character c from left to right:
if c is a space:
if result is not empty:
pendingSpace = true
else:
if pendingSpace:
append one space to result
pendingSpace = false
append c to resultWhen i arrives after the spaces at indices 5-7, the flag causes one space to be appended before i. The same rule turns indices 10-11 into one space before f. The final spaces at indices 15-16 set the flag, but no later non-space character arrives, so the flag is never acted on and no trailing space is appended.
Supply the missing condition so a space can set pendingSpace only after some non-space output exists.
if (c == ' ') {
if (__________) {
pendingSpace = true;
}
}Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
Continue the same scan. After the first separator is appended before i, result becomes "DSA i", then "DSA is". The spaces at indices 10-11 make one separator pending, and f causes it to be appended, giving "DSA is f". u and n are copied directly, producing "DSA is fun". The final pending separator is discarded because the scan ends.
| INPUT REACHED | RESULT | PENDINGSPACE |
|---|---|---|
| leading spaces | "" | false |
| DSA | "DSA" | false |
| spaces after DSA | "DSA" | true |
| is | "DSA is" | false |
| spaces after is | "DSA is" | true |
| fun | "DSA is fun" | false |
| trailing spaces | "DSA is fun" | true |
Each rule has one job: copying a non-space preserves characters, delaying a separator compresses an internal space run, checking that result is not empty rejects leading spaces, and waiting for a later character rejects trailing spaces. The scan examines each of the 17 characters once, so its time complexity is O(n). The constructed result can hold up to O(n) characters, so the additional space complexity is O(n) in the worst case.