Drill

ProblemsC++ › payments

Mask a card number

easypaymentsStringsC++

Support staff can see the last four digits of a card and nothing else.

maskCard(cardNo: 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 maskCard(std::string cardNo) {
    
}

Worked examples

CallResult
maskCard(std::string("4506349012345678"))std::string("************5678")
maskCard(std::string("12345"))std::string("*2345")
maskCard(std::string("1234"))std::string("1234")
maskCard(std::string(""))std::string("")

Hint

Build the asterisks from the length, then glue on the tail.

Reference solution in C++
std::string maskCard(std::string cardNo) {
    if (cardNo.size() <= 4) return cardNo;
    return string(cardNo.size() - 4, '*') + cardNo.substr(cardNo.size() - 4);
}

The same problem in another language

More payments problems in C++