Drill

ProblemsC++ › text

Hide most of an email address

easytextStringsParsingC++

A support screen shows enough of the address for an agent to recognise it, without putting the whole thing on screen.

maskEmail(address: 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 maskEmail(std::string address) {
    
}

Worked examples

CallResult
maskEmail(std::string("ahmet@example.com"))std::string("a****@example.com")
maskEmail(std::string("a@b.com"))std::string("a@b.com")
maskEmail(std::string("noatsign"))std::string("noatsign")
maskEmail(std::string("@x.com"))std::string("@x.com")

Hint

Find the @ first. Everything follows from where it is.

Reference solution in C++
std::string maskEmail(std::string address) {
    size_t at = address.find('@');
    if (at == string::npos || at == 0) return address;
    return address.substr(0, 1) + string(at - 1, '*') + address.substr(at);
}

The same problem in another language

More text problems in C++