Start learning free

LESSON 75 OF 292 · STRINGS - CHALLENGES

Initials in Swift: split, prefix and uppercased

Practice Read the full explanation

Given a person's full name fullName, made of a first name and a last name, return their initials: the first letter of each name, in upper case.

The two names are separated by a single space. The initials are always upper case, even when the name is written in lower case.

 

Example 1:

Input: fullName = "grace hopper"
Output: "GH"
Explanation: The first name starts with g and the last name with h. In upper case, they give "GH".

Example 2:

Input: fullName = "Ada Lovelace"
Output: "AL"
Explanation: A and L are already upper case, so they stay as they are.

Example 3:

Input: fullName = "a b"
Output: "AB"
Explanation: A name can be a single letter: that letter is its initial.

 

Constraints:

  • fullName has exactly two words, the first name and the last name, separated by one space.
  • Both names are made of English letters only, lower case or upper case.
STEP 1 OF 2
    Ctrl + Enter to check

    SWIFT · STRINGS - CHALLENGES

    Initials in Swift, explained

    Problem overview

    The function initials receives a full name, a first name and a last name separated by a single space, and returns the initials: the first letter of each name, in upper case.

    For "grace hopper" the answer is "GH", even though the name is written in lower case. For "Ada Lovelace" it is "AL": letters that are already upper case stay that way. For "a b" it is "AB", because a name can be a single letter and that letter is its initial.

    Approach: split, take one letter from each word, change the case

    The work has three parts. Split the full name at the space to get the two words. Take the first character of each word. Turn each of those characters into upper case and join them. Swift offers a method for each part: split(separator:), prefix(1) and uppercased().

    Step by step

    1. Split the name with fullName.split(separator: " ") and store the result in a constant named words. With exactly one space in the name, the array has exactly two items: words[0] is the first name and words[1] the last name.
    2. Take the first letter of the first word with words[0].prefix(1).
    3. Make it upper case with uppercased(), which returns a String.
    4. Do the same for the second word with words[1].prefix(1).uppercased().
    5. Join the two letters with + and return the result.

    The solution in Swift

    class Solution {
        func initials(_ fullName: String) -> String {
            let words = fullName.split(separator: " ")
            return words[0].prefix(1).uppercased() + words[1].prefix(1).uppercased()
        }
    }
    

    Details worth knowing:

    • Substrings all the way. split returns an array of Substring, and prefix(1) on a Substring returns another Substring. Here no String(...) wrapper is needed, because uppercased() returns a String, and joining two strings gives a String.
    • Array indexes work, string indexes don't. words[0] is fine because words is an array. You can't write word[0] on a string, which is why prefix(1) is used to get the first character.
    • Case conversion follows Unicode. uppercased() handles letters outside English too: "ñandu".prefix(1).uppercased() is "Ñ". A few characters grow when converted, such as "ß".uppercased(), which is "SS". The lesson only has English letters, so each initial is one letter.
    • Extra spaces are skipped. By default split drops empty parts, so "ada lovelace" with two spaces still gives "AL".
    • The words must exist. With a one-word name, words[1] is out of range, and the program stops at run time. The lesson always provides two words.

    Reading the solution as a trace

    For "grace hopper", the split gives the array ["grace", "hopper"], so words[0] is grace and words[1] is hopper. The first prefix(1) keeps g and the second keeps h. After uppercased() they are "G" and "H", and + joins them into "GH". Each word is handled by the same short expression, so the line reads left to right as two copies of one idea.

    Edge cases and common mistakes

    • Lower-case input. "grace hopper" must give "GH", so the upper-casing can't be skipped.
    • Mixed or full upper case. "ADA LOVELACE" gives "AL": only the first letter of each word is kept, so the rest of the name doesn't matter.
    • Single-letter names. "x Y" gives "XY".
    • Uppercasing the whole name first. This works but does more than needed; upper-casing only the two letters is enough.
    • Using suffix(1). That takes the last letter of each word, giving "ER" for "grace hopper".

    Testing

    • "grace hopper" gives "GH".
    • "Ada Lovelace" gives "AL".
    • "a b" gives "AB".
    • "ADA LOVELACE" gives "AL".
    • "x Y" gives "XY".
    Keep going with Swift1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free