Start learning free

LESSON 75 OF 291 · STRINGS - CHALLENGES

Initials in Scala: split, substring and toUpperCase

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

    SCALA · STRINGS - CHALLENGES

    Initials in Scala, explained

    Problem overview

    The method initials receives a person's full name, a first name and a last name separated by one space, and returns the first letter of each name in upper case. "grace hopper" gives "GH", even though the name is written in lower case. "Ada Lovelace" gives "AL", since those letters are already capitals. A name can be a single letter: "a b" gives "AB".

    Approach

    The task has three small jobs. First, separate the two names. Second, take the first letter of each. Third, turn the result into upper case. Splitting the full name at the space gives an array with the first name at index 0 and the last name at index 1. A slice of length one from the start of each name gives its first letter, and toUpperCase finishes the job.

    The order of the last two jobs matters little for the result, but the letters have to stay strings until they are joined. With substring each letter is a one-character String, so + glues them into "gh". Two Char values joined with + would be added up as numbers instead.

    Step by step

    1. Split the full name at the space with fullName.split(" "), and keep the array in a val named words. With exactly one space in the name, there are exactly two words. For "grace hopper" the array is [grace, hopper].
    2. Take the first letter of each word with words(0).substring(0, 1) and words(1).substring(0, 1). Scala reads an array item with parentheses.
    3. Join them and convert to upper case. Put the sum in parentheses, so that toUpperCase applies to the joined text and not only to the last letter, then return it.

    The solution in Scala

    class Solution {
        def initials(fullName: String): String = {
            val words = fullName.split(" ")
            return (words(0).substring(0, 1) + words(1).substring(0, 1)).toUpperCase
        }
    }
    

    A few Scala details:

    • A single space is a safe separator for split, because a space has no special meaning in a regular expression. Characters such as . would need escaping.
    • The parentheses matter. Without them, toUpperCase would be attached to the second slice alone, and "grace hopper" would give "gH".
    • toUpperCase returns a new string, because strings can't be changed in place. It leaves letters that are already upper case alone.
    • Scala allows calling toUpperCase without empty parentheses, as the lesson does. Writing toUpperCase() works too.

    Tracing an example

    For "grace hopper", the words are grace and hopper. The slices are "g" and "h", which join into "gh", and toUpperCase turns that into "GH". For "a b", the slices are "a" and "b", and the result is "AB".

    Edge cases and common mistakes

    • Using Chars. words(0).charAt(0) + words(1).charAt(0) adds two character codes and gives a number. Keep the letters as strings.
    • Forgetting the parentheses. Only the last letter would be converted, so "Ada Lovelace" works by luck, but "grace hopper" doesn't.
    • Single-letter names. "a b" works without any special case: the slice of length one is the whole word.
    • Extra spaces. The method relies on exactly one space. Two spaces would put an empty word at index 1 of the array, and taking a slice of length one from it throws an exception. The lesson's constraints rule this out.

    Testing

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