Opening the reading…
Opening the reading…
DSA FUNDAMENTALS › STRING BASICS
Start with the string "planet". If you remove its first character, the result is "lanet". If you remove its last character, the result is "plane". If you remove its third character, the result is "plnet". Each result has length 5 because one character was removed from the original length of 6.
The characters that remain do not get rearranged. In "planet", removing the first character makes l, a, n, e, and t shift left by one slot. Removing the third character makes n, e, and t shift left, while p and l stay where they are. Their character order is still p, l, n, e, t.
| CHARACTER REMOVED | RESULT | RESULT LENGTH |
|---|---|---|
| First character | lanet | 5 |
| Last character | plane | 5 |
| Third character | plnet | 5 |
The six characters in "planet" have one-based positions for people and zero-based indices for code. Position 1 is index 0, position 2 is index 1, and so on. Therefore the first character is at index 0, the last character is at index n - 1, and the kth character is at index k - 1.
Characters: p l a n e t
Positions: 1 2 3 4 5 6
Indices: 0 1 2 3 4 5
n = 6
k = 3
kth position 3 -> index k - 1 -> index 2 -> 'a'
index 3 would select 'n', the fourth positionWhich character and result come from removing k = 3 from "planet"?
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
In C++, string.erase(index, count) removes count characters starting at index. To remove only the third character from a fresh copy of "planet", use erase(k - 1, 1). The first argument converts position 3 to index 2, and the second argument says to remove exactly one character.
string first = "planet";
first.erase(0, 1); // "lanet"
string last = "planet";
last.erase(n - 1, 1); // "plane"
string kth = "planet";
kth.erase(k - 1, 1); // "plnet"The count is not optional in meaning, even when the language permits you to leave it out. erase(k - 1) removes everything from index k - 1 to the end, so on "planet" it removes "anet" and leaves "pl". That is a suffix deletion, not a one-character deletion.
StringBuilder first = new StringBuilder("planet");
first.deleteCharAt(0); // "lanet"
StringBuilder last = new StringBuilder("planet");
last.deleteCharAt(n - 1); // "plane"
StringBuilder kth = new StringBuilder("planet");
kth.deleteCharAt(k - 1); // "plnet"Correct this C++ line so a fresh copy of "planet" removes only its third character.
s.erase(k);Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
For "planet", n is 6, so a valid kth position satisfies 1 <= k <= 6. The chosen value k = 3 is valid because it points to 'a' at index 2. A value below 1 or above 6 does not identify a character in this string.
First and last removal also require a non-empty string. The first index is 0 and the last index is n - 1, but an empty string has no character at either index. Passing an invalid deletion index can throw an exception or trigger a language-specific failure instead of producing a valid result.