Problem overview
The method Stutterize receives a word and returns it said with a stutter: its first two characters, a hyphen, then the whole word. "dog" becomes "do-dog", "Hello" becomes "He-Hello", and the two-letter word "ab" becomes "ab-ab". The stutter keeps the case of the word.
Approach: three pieces
The result is always three pieces in a fixed order: the first two characters, the text "-", and the word itself. Only the first piece needs any work, and Substring does it. word.Substring(0, 2) starts at index 0 and takes 2 characters. The second argument is a count of characters, not an end position.
Console.WriteLine("dog".Substring(0, 2));
Console.WriteLine("Hello".Substring(0, 2));
Console.WriteLine("ab".Substring(0, 2));
do
He
ab
For "ab", the first two characters are the whole word, which is why the result is "ab-ab".
The solution in C#
class Solution {
public string Stutterize(string word) {
return word.Substring(0, 2) + "-" + word;
}
}
All three pieces are strings, so + joins them as text. Each + creates a new string; word itself is never changed, because strings are immutable.
Step by step
The lesson is one step. Take "Hello" as an example and build the answer in the order of the pieces.
- Take the first two characters.
"Hello".Substring(0, 2)is"He". - Add the hyphen. A string literal of one character,
"-". The result so far is"He-". - Add the whole word. The original
word, untouched, giving"He-Hello".
All of this fits in one return because + is evaluated from left to right and every operand is a string. There is no loop and no condition: the same three pieces are used for every word.
Other ways to write it
String interpolation expresses the same three pieces as a single template, where each part in braces is evaluated and inserted. A range can also replace Substring: word[..2] means "everything before index 2".
string result = $"{word[..2]}-{word}";
For "Hello" this gives "He-Hello" as well. The lesson's solution with Substring and + makes the structure easier to see when you are learning how strings are cut and joined: a slice, a separator, and the original word.
Edge cases and common mistakes
- A word shorter than 2 characters. The lesson guarantees at least 2 letters. For a one-letter word such as
"a",Substring(0, 2)throws anArgumentOutOfRangeException. - Using
Substring(0, 1)orSubstring(1, 2). The first takes only one character; the second starts at the wrong place. The stutter is index 0, length 2. - Changing the case. Nothing is converted, so the capital in
"Hello"appears in both parts. - Forgetting the hyphen's quotes. The hyphen is text, so it goes in double quotes:
"-".
It helps to read the result aloud: the two characters are the beginning of a word, and the stutter repeats them before the whole word is said. That is why the second and third examples show the same letters twice, "do-dog" and "ab-ab". In the second one the stutter and the word are the same string, because the word has exactly two letters. The method does not need a special case for it, since Substring(0, 2) on a two-character string simply returns all of it.
Testing
| word | Result |
|---|---|
"dog" | "do-dog" |
"Hello" | "He-Hello" |
"ab" | "ab-ab" |
