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

std::string initials(std::string fullName) {
    
}

Worked examples

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

Hint

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

Reference solution in C++
std::string initials(std::string fullName) {
    string out;
    istringstream in(fullName);
    string w;
    while (in >> w) {
        out += toupper(static_cast<unsigned char>(w[0]));
        out += '.';
    }
    return out;
}

The same problem in another language

More warmup problems in C++