Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Disclosure: My content contains affiliate links.

43,406 questions

56,397 answers

573 users

How to mask a number with stars except the last 4 digits in C

4 Answers

0 votes
#include <stdio.h>
#include <string.h>

void mask_number_with_stars_except_the_last_4_digits(char cardNumber[], char maskedNumber[]) {
    char last4Digits[5];
    
    strncpy(last4Digits, cardNumber + strlen(cardNumber) - 4, 4);
    last4Digits[4] = '\0';
    
    memset(maskedNumber, '*', strlen(cardNumber) - 4);
    strcpy(maskedNumber + strlen(cardNumber) - 4, last4Digits);
}

int main() {
    char cardNumber[] = "9003125334656789";
    char maskedNumber[17] = "";
    
    mask_number_with_stars_except_the_last_4_digits(cardNumber, maskedNumber);
    
    printf("%s\n", maskedNumber);
    
    return 0;
}



/*
run:

************6789

*/

 



answered May 29, 2024 by avibootz
0 votes
#include <stdio.h>
#include <string.h>

/**
 * Masks all characters in a string with '*' except the last `visible_count` characters.
 * Operates directly on the input buffer (in-place).
 */
void mask_number_inplace(char *str, size_t visible_count) {
    if (!str) return;

    size_t len = strlen(str);

    // No masking needed if the string is too short
    if (len <= visible_count) return;

    size_t mask_len = len - visible_count;

    // Replace leading characters with '*'
    for (size_t i = 0; i < mask_len; i++) {
        str[i] = '*';
    }
}

int main(void) {
    char card[] = "1234567890123456";
    char short_code[] = "123";

    mask_number_inplace(card, 4);
    mask_number_inplace(short_code, 4);

    printf("Masked card:       %s\n", card);       // ************3456
    printf("Masked short code: %s\n", short_code); // 123

    return 0;
}



/*
run:

Masked card:       ************3456
Masked short code: 123

*/

 



answered 6 hours ago by avibootz
0 votes
#include <stdio.h>
#include <string.h>

/**
 * Copies `src` to `dest` with all leading characters masked except the last `visible_count`.
 * Ensures null-termination and prevents buffer overflow.
 */
void mask_number_copy(const char *src, char *dest, size_t dest_size, size_t visible_count) {
    if (!src || !dest || dest_size == 0) return;

    size_t len = strlen(src);

    // Ensure destination buffer is large enough (length + null terminator)
    if (dest_size <= len) {
        dest[0] = '\0'; // Safety clear
        return;
    }

    size_t mask_len = (len > visible_count) ? (len - visible_count) : 0;

    for (size_t i = 0; i < len; i++) {
        if (i < mask_len) {
            dest[i] = '*';
        } else {
            dest[i] = src[i];
        }
    }

    dest[len] = '\0'; // Null-terminate
}

int main(void) {
    const char *original = "98765432101234";
    char masked[32];

    mask_number_copy(original, masked, sizeof(masked), 4);

    printf("Original: %s\n", original); // 98765432101234
    printf("Masked:   %s\n", masked);   // **********1234

    return 0;
}



/*
run:

Original: 98765432101234
Masked:   **********1234

*/

 



answered 6 hours ago by avibootz
0 votes
#include <stdio.h>
#include <string.h>
#include <ctype.h>

void mask_digits_keep_format(char *str, size_t visible_digits) {
    if (!str) return;

    // Count total digits in the string
    size_t total_digits = 0;
    for (size_t i = 0; str[i] != '\0'; i++) {
        if (isdigit((unsigned char)str[i])) {
            total_digits++;
        }
    }

    if (total_digits <= visible_digits) return;

    size_t digits_to_mask = total_digits - visible_digits;
    size_t masked_count = 0;

    // Mask digits from left to right until digits_to_mask limit is reached
    for (size_t i = 0; str[i] != '\0'; i++) {
        if (isdigit((unsigned char)str[i])) {
            if (masked_count < digits_to_mask) {
                str[i] = '*';
                masked_count++;
            }
        }
    }
}

int main(void) {
    char formatted[] = "1234-5678-9012-3456";
    mask_digits_keep_format(formatted, 4);

    printf("Formatted: %s\n", formatted); // ****-****-****-3456
    
    return 0;
}


/*
run:

Formatted: ****-****-****-3456

*/

 



answered 6 hours ago by avibootz

Related questions

...