Table of Contents

    strnset() function in C Programming Language: Set Characters in String to Specific Value in C

    strnset() function in C Programming Language: Set Characters in String to Specific Value in C

    strnset() function sets portion of characters in a string to given character. Syntax for strnset( ) function is given below.

    Syntax

    char *strnset(char *string, int c);

    Important Note

    strnset() function is non standard function which may not available in standard library in C.

    Program

    In this program, first 4 characters of the string Welcome to atnyla is set to "#" using strnset( ) function and output is displayed as ######e to atnyla.

    
    #include<stdio.h>
    #include<string.h>
    int main()
    {
        char str[20] = "Welcome to atnyla";
        printf("Original string is : %s \n", str);
        printf("Test string after string n set" \
               " : %s \n", strnset(str,'#',6));
        printf("After string n set : %s \n", str);
        return 0;
    }
    

    Output

    Original string is : Welcome to atnyla
    Test string after string n set : ######e to atnyla
    After string n set : ######e to atnyla
    Press any key to continue . . .