Drill

ProblemsJava › warmup

Initials for an avatar

easywarmupStringsParsingJava

The user menu shows initials when someone has no profile picture.

initials(fullName: string) → string

Java needs a compiler and Drill does not host one yet, so this page is the reference rather than an exercise: the problem, worked examples, and the solution in full. To type it out, the same problem runs in Python.

Solve it in Python →

Where you start

String initials(String fullName) {
    
}

Worked examples

CallResult
initials("ada lovelace")"A.L."
initials("Grace Brewster Hopper")"G.B.H."
initials(" linus ")"L."
initials("")""

Hint

Split on whitespace, drop the empty pieces, then take index 0 of each.

Reference solution in Java
String initials(String fullName) {
    StringBuilder out = new StringBuilder();
    for (String w : fullName.trim().split("\\s+")) {
        if (!w.isEmpty()) out.append(Character.toUpperCase(w.charAt(0))).append('.');
    }
    return out.toString();
}

The same problem in another language

More warmup problems in Java