Drill

ProblemsC# › warmup

Initials for an avatar

easywarmupStringsParsingC#

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

Initials(fullName: string) → string

C# 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

public 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 C#
public string Initials(string fullName) {
    var sb = new StringBuilder();
    foreach (var w in fullName.Split((char[]) null, StringSplitOptions.RemoveEmptyEntries)) {
        sb.Append(char.ToUpperInvariant(w[0])).Append('.');
    }
    return sb.ToString();
}

The same problem in another language

More warmup problems in C#