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

public string MaskCard(string cardNo) {
    
}

Worked examples

CallResult
MaskCard("4506349012345678")"************5678"
MaskCard("12345")"*2345"
MaskCard("1234")"1234"
MaskCard("")""

Hint

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

Reference solution in C#
public string MaskCard(string cardNo) {
    if (cardNo.Length <= 4) return cardNo;
    return new string('*', cardNo.Length - 4) + cardNo.Substring(cardNo.Length - 4);
}

The same problem in another language

More payments problems in C#