Start learning free

LESSON 76 OF 285 · STRINGS - CHALLENGES

Initials in Go: strings.Split and strings.ToUpper

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

    GO · STRINGS - CHALLENGES

    Initials in Go, explained

    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 of s between the separators.
    • A slice expression such as word[0:1] returns the first letter as a one-character string.
    • strings.ToUpper(s) returns s with its letters in upper case.

    Step by step

    1. Split at the space into a slice named words with strings.Split(fullName, " "). With exactly one space in the name, there are exactly two words.
    2. 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.
    3. Join the two letters with + and pass the result to strings.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 a byte, a number. The slice words[0][0:1] is a string, which + and strings.ToUpper accept directly.
    • Letters that are already upper case stay as they are. strings.ToUpper changes only lower-case letters, so "Ada Lovelace" loses nothing.
    • Convert once. Calling ToUpper on the joined pair, instead of on each letter, needs one call and gives the same result.
    • The strings package. In a file of your own, add import "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".
    Keep going with Go1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free