Start learning free

LESSON 71 OF 292 · STRINGS - CHALLENGES

Stuttering Function in Swift: prefix and String Joining

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

    SWIFT · STRINGS - CHALLENGES

    Stuttering Function in Swift, explained

    Problem overview

    The function stutterize receives a word and returns it said with a stutter: its first two characters, then a hyphen, then the whole word. The word has at least two letters and is made of English letters only.

    For "dog" the first two characters are do, so the result is "do-dog". For "Hello" it is "He-Hello": the case is kept, so the stutter starts with a capital H. For "ab" the first two characters are the whole word, and the result is "ab-ab".

    Approach: three pieces joined together

    The answer is built from three parts that never change order: the first two characters, the text "-" and the word itself. The only part that needs real work is the first. Swift strings can't be indexed with integers, so word[0] isn't available. The method prefix(2) does the job: it keeps the first two characters and doesn't need the length.

    Step by step

    1. Take the first two characters with word.prefix(2).
    2. Convert them to a String. prefix returns a Substring, and wrapping it in String(...) makes a string you can build on.
    3. Add the hyphen as the string "-".
    4. Add the whole word and return the three pieces joined with +.

    The solution in Swift

    class Solution {
        func stutterize(_ word: String) -> String {
            return String(word.prefix(2)) + "-" + word
        }
    }
    

    Details worth knowing:

    • Why the conversion is there. prefix(2) returns a Substring, not a String. Swift can join a Substring with a String using +, but wrapping the piece in String(...) says plainly what type you are building, and it is the habit that matters when both sides are substrings: word.prefix(2) + word.suffix(1) would give a Substring, which can't be returned from a function declared to return a String.
    • The word is used as it is. The third piece is the parameter itself, with no conversion: it is already a String.
    • Short words are fine. If the word has fewer than two characters, prefix(2) returns the whole word instead of crashing. The lesson guarantees two letters, so for "ab" the prefix is the entire word.
    • String joining. + creates a new string each time. For a few pieces this is fine; for hundreds, building with append or an interpolated string is a better habit.

    String interpolation gives an equivalent version: "\(word.prefix(2))-\(word)". Interpolation turns the Substring into text automatically, so no String(...) is needed. The lesson's version uses + to practise joining.

    Building the word piece by piece

    Each part of the result has its own job, and you can see them one by one for "banana": the stutter is ba, the separator is -, and the word is banana, so the result is "ba-banana". The stutter is only ever as long as two characters, however long the word is, while the final part grows with the word. That is why the result of a 6-letter word has 9 characters and the result of "ab" has 5.

    Edge cases and common mistakes

    • Two-letter words. "ab" gives "ab-ab".
    • Keeping the case. Don't change letters: "Hello" gives "He-Hello", not "he-Hello".
    • Using prefix(1). One character gives "d-dog", which is a different stutter.
    • Dropping the hyphen or adding spaces. The separator is exactly -, with no spaces around it.

    Testing

    • "dog" gives "do-dog".
    • "Hello" gives "He-Hello".
    • "ab" gives "ab-ab".
    • "Be" gives "Be-Be".
    • "banana" gives "ba-banana".
    Keep going with Swift1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free