Start learning free

LESSON 75 OF 291 · STRINGS - CHALLENGES

Initials in Kotlin: split, substring and uppercase

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

    KOTLIN · STRINGS - CHALLENGES

    Initials in Kotlin, explained

    Problem overview

    The function initials receives a full name 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 answer is "GH". For "Ada Lovelace" it is "AL", since the letters are already capitals. For "a b" it is "AB": a name can be one letter long.

    Approach

    There are three small jobs: separate the two names, take the first letter of each, and convert the result to upper case. split(" ") separates the names into a list. substring(0, 1) takes the first letter of a name as a String. uppercase() converts a string to upper case.

    Step by step

    1. Split the full name at the space into a list named words, declared with val. With exactly one space, there are exactly two words.
    2. Take the first letter of each word with substring(0, 1), add the two letters with +, put the sum in parentheses and call uppercase() on it. The parentheses make the conversion apply to both letters. Without them, it would apply only to the second one.

    The solution in Kotlin

    class Solution {
        fun initials(fullName: String): String {
            val words = fullName.split(" ")
            return (words[0].substring(0, 1) + words[1].substring(0, 1)).uppercase()
        }
    }
    

    Details worth knowing:

    • Why substring(0, 1). Indexing, as in words[0][0], gives a Char, and two Char values can't be added into a string. A one-letter slice is a String, so + concatenates the letters.
    • Two kinds of brackets. words[0] reads an item of the list, and substring is called on that item. Reading left to right: the first word, then its first letter.
    • uppercase() returns a new string. The original is unchanged. In Kotlin 1.5 and later this is the function to use, and it replaces the older toUpperCase().
    • Letters that are already capitals stay capitals. Converting "AL" to upper case gives "AL", which is why one call handles both "grace hopper" and "Ada Lovelace".

    Tracing the examples

    For "grace hopper", the split gives [grace, hopper]. The first letters are "g" and "h", their sum is "gh", and uppercase() turns it into "GH".

    For "Ada Lovelace", the letters are "A" and "L", and the result is "AL".

    For "a b", each word has one letter, so substring(0, 1) is the whole word. The result is "AB".

    Another way to write it

    The same result can come from a transformation of the list: fullName.split(" ").map { it.substring(0, 1) }.joinToString("").uppercase() takes the first letter of every word, glues them with an empty separator and converts the text. For "grace hopper" it gives "GH". It would also work for three names, while the reference solution is written for exactly two. For a two-name exercise, the reference solution is shorter to read and easier to trace.

    Edge cases and common mistakes

    • Converting only one letter. words[0].substring(0, 1) + words[1].substring(0, 1).uppercase() converts just the second letter, because the call binds to the nearest piece. For "grace hopper" it would give "gH".
    • A single word. "grace".split(" ") gives a list with one item, so words[1] throws an IndexOutOfBoundsException. The lesson guarantees two words.
    • Extra spaces. Two spaces in a row would produce an empty word in the middle. The lesson promises a single space.
    • Special cases of upper-casing. Some letters change length when converted: "ß".uppercase() is "SS". The lesson uses English letters only, so every initial stays one letter.

    Testing

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