Usually when you buy something, you’re asked whether your credit card number, phone number or answer to your most secret question is still correct. However, since someone could look over your shoulder, you don’t want that shown on your screen. Instead, we mask it.
Your task is to write a function maskify
, which changes all but the last four characters into '#'
.
The problem link is here.
Examples
"4556364607935616" --> "############5616"
"64607935616" --> "#######5616"
"1" --> "1"
"" --> ""
// "What was the name of your first pet?"
"Skippy" --> "##ippy"
"Nananananananananananananananana Batman!"
-->
"####################################man!"
Solution
#include <string.h>
char *maskify(char *masked, const char *string)
{
unsigned long i, slen = strlen(string);
strcpy(masked, string);
if (slen > 4)
for (i = 0; i < slen - 4; i++)
masked[i] = '#';
return masked; // return it
}