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
- Split the full name at the space with
fullName.split(" "), and keep the array in avalnamedwords. With exactly one space in the name, there are exactly two words. For"grace hopper"the array is[grace, hopper]. - Take the first letter of each word with
words(0).substring(0, 1)andwords(1).substring(0, 1). Scala reads an array item with parentheses. - Join them and convert to upper case. Put the sum in parentheses, so that
toUpperCaseapplies 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,
toUpperCasewould be attached to the second slice alone, and"grace hopper"would give"gH". toUpperCasereturns a new string, because strings can't be changed in place. It leaves letters that are already upper case alone.- Scala allows calling
toUpperCasewithout empty parentheses, as the lesson does. WritingtoUpperCase()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".
