strset() and a Portable Character-Replacement Function

strset() is a non-standard extension that replaces every character of a string with one chosen character while preserving the terminating null character. The same behavior is easy to implement portably.

Portability: strset() is not an ISO C standard-library function.

Legacy Syntax Top ↑

strset(string, '*');  // non-standard

Portable Replacement Top ↑

#include <stdio.h>

void set_all_chars(char text[], char replacement) {
    for (size_t i = 0; text[i] != '\0'; i++) {
        text[i] = replacement;
    }
}

int main(void) {
    char text[] = "plus2net";

    set_all_chars(text, '*');
    printf("%s\n", text);

    return 0;
}

Output

********

Empty String Behavior Top ↑

#include <stdio.h>

void set_all_chars(char text[], char replacement) {
    for (size_t i = 0; text[i] != '\0'; i++) {
        text[i] = replacement;
    }
}

int main(void) {
    char empty[] = "";

    set_all_chars(empty, '#');
    printf("Length remains zero: \"%s\"\n", empty);

    return 0;
}

Preserved Edge-Case Variations Top ↑

Null-Safe Portable Replacement

#include <stdio.h>

int set_all_chars(char *text, char replacement) {
    if (text == NULL) {
        return 0;
    }

    while (*text != '\0') {
        *text = replacement;
        text++;
    }

    return 1;
}

int main(void) {
    char *text = NULL;

    if (!set_all_chars(text, '*')) {
        printf("String is NULL.\n");
    }

    return 0;
}

Replace Every Character in a String Containing Punctuation and Digits

#include <stdio.h>

void set_all_chars(char text[], char replacement) {
    for (size_t i = 0; text[i] != '\0'; i++) {
        text[i] = replacement;
    }
}

int main(void) {
    char text[] = "Hello, World! 123";

    set_all_chars(text, '#');
    printf("%s\n", text);

    return 0;
}

Output

#################




plus2net.com






✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer