Hide most of an email address
A support screen shows enough of the address for an agent to recognise it, without putting the whole thing on screen.
- Keep the first character of the part before the @, replace the rest of that part with asterisks, and keep the domain as it is.
- A single-character local part has nothing to hide, so it comes back unchanged.
- Anything without an @, or with nothing before it, comes back untouched.
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.
Where you start
std::string maskEmail(std::string address) {
}
Worked examples
| Call | Result |
|---|---|
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);
}