Problem overview
The method initials receives a full name made of a first name and a last name, separated by a single space, and returns the first letter of each name in upper case. The initials are upper case even when the name is written in lower case. Both names consist of English letters only, and a name can be a single letter.
For "grace hopper" the result is "GH". For "Ada Lovelace" it is "AL", since those letters are already upper case. For "a b" it is "AB".
Approach: split into words, take two letters, convert
The job divides into three small pieces:
- separate the first name from the last name;
- take the first letter of each;
- join the two letters and convert them to upper case.
The method split handles the first piece. fullName.split(" ") cuts the string at each space and returns a String[]. With exactly one space, there are exactly two words: words[0] is the first name and words[1] is the last. Array items are numbered from 0 and read with square brackets.
For the letters, substring(0, 1) returns the first character as a one-character String. The start index is included and the end index is excluded, so the slice holds only index 0.
The choice of substring over charAt matters. charAt(0) returns a char, which is a number underneath, and adding two char values with + gives an int: 'g' + 'h' is 207. Two strings, in contrast, are joined as text.
Step by step
- Split at the space.
fullName.split(" ")gives[grace, hopper]for"grace hopper". Store it in aString[]namedwords. - Take the first letter of each word.
words[0].substring(0, 1)andwords[1].substring(0, 1)are one-letter strings. - Join them and convert. Put the sum in parentheses and call
toUpperCase()on it. The parentheses make the conversion apply to both letters. Without them,toUpperCase()would bind only to the last slice, and the first letter would keep its case. - Return the result.
toUpperCase()returns a new string, and the method returns it.
The solution in Java
class Solution {
public String initials(String fullName) {
String[] words = fullName.split(" ");
return (words[0].substring(0, 1) + words[1].substring(0, 1)).toUpperCase();
}
}
On the three examples it returns:
grace hopper -> GH
Ada Lovelace -> AL
a b -> AB
A few Java details here:
- Strings are immutable.
toUpperCase()doesn't change the string it is called on. It returns a new one, so its result must be returned or stored. - Upper-case letters stay as they are. Converting
"AL"returns"AL"again, so the code needs no check of the current case. - A space is not a special regular expression character, so
split(" ")needs no escaping, unlikesplit(".").
Edge cases and common mistakes
- Adding
charvalues.words[0].charAt(0) + words[1].charAt(0)is anint, not text. Slices keep the letters as strings, and+concatenates them. - Converting only one letter.
words[0].substring(0, 1).toUpperCase() + words[1].substring(0, 1)upper-cases the first initial only, so"grace hopper"would give"Gh". Converting the joined text fixes both at once. - Single-letter names. In
"a b"each word has length 1, andsubstring(0, 1)returns the whole word, so the result is"AB". - More than one space. The lesson guarantees one space. If a name had two, as in
"grace hopper",split(" ")would return three items,[grace, , hopper], andwords[1]would be the empty string, sosubstring(0, 1)would throw an exception. - A name with no space.
"grace".split(" ")has one item, sowords[1]fails with anArrayIndexOutOfBoundsExceptionand the messageIndex 1 out of bounds for length 1.
Testing
"grace hopper"gives"GH"."Ada Lovelace"gives"AL"."a b"gives"AB".
