Show only the last four digits
A payment dashboard shows card numbers on receipts. To protect customers, every digit except the last four digits must be replaced by an asterisk *. Spaces, dashes and any other characters stay exactly where they are, so the card still looks like it was typed.
For example 4111-1111-1111-1234 becomes ****-****-****-1234. If the text contains four digits or fewer, nothing is hidden.
Task: read one line holding a card number written in any style (digits possibly separated by spaces or dashes). Print the masked version of that line. Only the digits are counted when deciding which four are the last four.
Sample tests
Input
4111-1111-1111-1234
Expected output
****-****-****-1234
Input
5500 0000 0000 0004
Expected output
**** **** **** 0004
Your answer is also checked against 3 hidden tests.
Need a hint?
Count the digits first, then walk the string again and decide for each digit whether it is among the last four.
Log in or create a free account to run your code, submit and earn points.
Run your code to see the output here. Ctrl + Enter also runs it.
