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
- Split the name with
fullName.split(separator: " ")and store the result in a constant namedwords. With exactly one space in the name, the array has exactly two items:words[0]is the first name andwords[1]the last name. - Take the first letter of the first word with
words[0].prefix(1). - Make it upper case with
uppercased(), which returns aString. - Do the same for the second word with
words[1].prefix(1).uppercased(). - 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.
splitreturns an array ofSubstring, andprefix(1)on aSubstringreturns anotherSubstring. Here noString(...)wrapper is needed, becauseuppercased()returns aString, and joining two strings gives aString. - Array indexes work, string indexes don't.
words[0]is fine becausewordsis an array. You can't writeword[0]on a string, which is whyprefix(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
splitdrops 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".
