Problem overview
The function initials receives a full name, fullName, made of a first name and a last name separated by one space. It returns the first letter of each name, in upper case.
For "grace hopper" the result is "GH": the names start with g and h, and the initials are upper case even when the name is written in lower case. "Ada Lovelace" gives "AL". A name can be a single letter, so "a b" gives "AB".
Approach
The two names are separated by a space, so splitting the full name at the space gives the two words. The initial of each word is its first letter. Join the two letters and convert the pair to upper case.
Three tools from the standard library and the language cover it:
strings.Split(s, sep)returns a slice of the pieces ofsbetween the separators.- A slice expression such as
word[0:1]returns the first letter as a one-character string. strings.ToUpper(s)returnsswith its letters in upper case.
Step by step
- Split at the space into a slice named
wordswithstrings.Split(fullName, " "). With exactly one space in the name, there are exactly two words. - Take the first letter of each word.
words[0][0:1]works in two moves: the first index picks the word, and the slice[0:1]takes its first letter as a string.words[1][0:1]does the same for the last name. - Join the two letters with
+and pass the result tostrings.ToUpper.
The solution in Go
func initials(fullName string) string {
words := strings.Split(fullName, " ")
return strings.ToUpper(words[0][0:1] + words[1][0:1])
}
Running it on the three examples gives these results:
"grace hopper" -> "GH"
"Ada Lovelace" -> "AL"
"a b" -> "AB"
Details worth knowing:
- Slice, not index, for a string.
words[0][0]would be abyte, a number. The slicewords[0][0:1]is astring, which+andstrings.ToUpperaccept directly. - Letters that are already upper case stay as they are.
strings.ToUpperchanges only lower-case letters, so"Ada Lovelace"loses nothing. - Convert once. Calling
ToUpperon the joined pair, instead of on each letter, needs one call and gives the same result. - The
stringspackage. In a file of your own, addimport "strings". In the lesson's editor, the import is already there. - Bytes, not characters. A slice picks bytes. For English letters, which is what the constraints promise, one letter is one byte.
Edge cases and common mistakes
- Single-letter names.
"a b"splits into"a"and"b", and[0:1]is the whole word. This works because the slice end of 1 doesn't go past a one-letter word. - Forgetting the upper case. Without
strings.ToUpper,"grace hopper"would give"gh". - Splitting at the wrong separator.
strings.Split(fullName, "")would cut the name into single characters. The separator here is the space," ". - Index out of range. The code reads
words[1], so it needs two words. The constraints promise exactly two. With one word,words[1]stops the program with an "index out of range" panic. - Two spaces or a leading space. Extra spaces would create empty pieces and break the indexes. The constraints rule that out too.
Testing
"grace hopper"gives"GH"."Ada Lovelace"gives"AL"."a b"gives"AB".
