Start learning free

LESSON 70 OF 289 · STRINGS - CHALLENGES

Stuttering Function in C#: Substring and a Hyphen

Practice Read the full explanation

Given a word word, return it said with a stutter: its first two characters, then a hyphen -, then the whole word.

 

Example 1:

Input: word = "dog"
Output: "do-dog"
Explanation: The first two characters are "do". After them come a hyphen and the whole word, "dog".

Example 2:

Input: word = "Hello"
Output: "He-Hello"
Explanation: The characters keep their case: the stutter starts with an upper-case H, as the word does.

Example 3:

Input: word = "ab"
Output: "ab-ab"
Explanation: A word with only two letters: its first two characters are the whole word.

 

Constraints:

  • word has at least 2 letters.
  • word is made of English letters only, lowercase or uppercase: no spaces, digits or punctuation.
STEP 1 OF 1
    Ctrl + Enter to check

    C# · STRINGS - CHALLENGES

    Stuttering Function in C#, explained

    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.

    1. Take the first two characters. "Hello".Substring(0, 2) is "He".
    2. Add the hyphen. A string literal of one character, "-". The result so far is "He-".
    3. 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 an ArgumentOutOfRangeException.
    • Using Substring(0, 1) or Substring(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

    wordResult
    "dog""do-dog"
    "Hello""He-Hello"
    "ab""ab-ab"
    Keep going with C#1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free