Start learning free

LESSON 74 OF 285 · STRINGS - CHALLENGES

Initials in Java: 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

    JAVA · STRINGS - CHALLENGES

    Initials in Java, explained

    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:

    1. separate the first name from the last name;
    2. take the first letter of each;
    3. 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

    1. Split at the space. fullName.split(" ") gives [grace, hopper] for "grace hopper". Store it in a String[] named words.
    2. Take the first letter of each word. words[0].substring(0, 1) and words[1].substring(0, 1) are one-letter strings.
    3. 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.
    4. 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, unlike split(".").

    Edge cases and common mistakes

    • Adding char values. words[0].charAt(0) + words[1].charAt(0) is an int, 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, and substring(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], and words[1] would be the empty string, so substring(0, 1) would throw an exception.
    • A name with no space. "grace".split(" ") has one item, so words[1] fails with an ArrayIndexOutOfBoundsException and the message Index 1 out of bounds for length 1.

    Testing

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