Opening the reading…
Opening the reading…
DSA FUNDAMENTALS › STRING BASICS
Take the string "LeArnYard 7!". Each of its 12 characters is handled on its own. An uppercase letter becomes lowercase, a lowercase letter becomes uppercase, and every nonletter stays unchanged. The result is "lEaRNyARD 7!", where the letters changed case but the space, digit, and punctuation did not move or change.
| INPUT CHARACTER KIND | ACTION | RESULT IN "LEARNYARD 7!" |
|---|---|---|
| Uppercase letter | Convert to lowercase | L becomes l |
| Lowercase letter | Convert to uppercase | e becomes E |
| Nonletter | Leave unchanged | 7 remains 7 |
What should happen to '7' in "LeArnYard 7!"?
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
The offset depends on the direction of the conversion. If a character is from 'A' to 'Z', add 32 to its ASCII value to reach the matching lowercase letter. If it is from 'a' to 'z', subtract 32 to reach the matching uppercase letter. A character outside both ranges must skip both operations.
'L' is in 'A'-'Z': 76 + 32 = 108, so 'L' becomes 'l'
'e' is in 'a'-'z': 101 - 32 = 69, so 'e' becomes 'E'
'7' is in neither range: no offset is applied, so '7' stays '7'Start at index 0 and inspect one character at a time through index 11. The letters at indices 0 through 8 use one of the two range rules. At index 9 the space matches neither range, at index 10 '7' matches neither range, and at index 11 '!' matches neither range. No character must be searched for, and no character changes position.
| INDEX | INPUT | RULE | OUTPUT |
|---|---|---|---|
| 0 | L | Add 32 | l |
| 1 | e | Subtract 32 | E |
| 2 | A | Add 32 | a |
| 3 | r | Subtract 32 | R |
| 4 | n | Subtract 32 | N |
| 5 | Y | Add 32 | y |
| 6 | a | Subtract 32 | A |
| 7 | r | Subtract 32 | R |
| 8 | d | Subtract 32 | D |
| 9 | space | No change | space |
| 10 | 7 | No change | 7 |
| 11 | ! | No change | ! |
for (char& ch : text) {
if (ch >= 'A' && ch <= 'Z') {
ch += 32;
} else if (ch >= 'a' && ch <= 'z') {
ch -= 32;
}
}String text = "LeArnYard 7!";
StringBuilder converted = new StringBuilder(text);
for (int i = 0; i < converted.length(); i++) {
char ch = converted.charAt(i);
if (ch >= 'A' && ch <= 'Z') {
converted.setCharAt(i, (char) (ch + 32));
} else if (ch >= 'a' && ch <= 'z') {
converted.setCharAt(i, (char) (ch - 32));
}
}Complete the missing lowercase range guard in this conversion logic.
if (ch >= 'A' && ch <= 'Z') {
ch += 32;
} else if (????????) {
ch -= 32;
}Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
The converted string still has length 12. Index 0 contains the converted form of the original index 0, index 1 contains the converted form of the original index 1, and so on through index 11. Case conversion changes character values only, so the space stays at index 9, '7' stays at index 10, and '!' stays at index 11.
The pass visits each of the n characters once, so its time complexity is O(n). The guarded version also avoids corrupting lowercase letters with an unconditional addition and avoids corrupting the final space, digit, and punctuation with either offset. The same three outcomes are applied independently at every position.