Syntax
#include <string.h> char *strnset(char *string, int c, size_t n); char *strset(char *string, int c);Description
strnset sets, at most, the first n bytes of string to c (converted to a char). If n is greater than the length of string, the length of string is used in place of n. strset sets all bytes of string, except the ending null character (\0), to c (converted to a char).
For both functions, the string must be initialized and must end with a null character (\0).
Both strset and strnset return a pointer to the altered string. There is no error return value.
In this example, strnset sets not more than four bytes of a string to the byte 'x'. Then the strset function changes any non-null bytes of the string to the byte 'k'.
#include <stdio.h>
#include <string.h>
int main(void)
{
char *str = "abcdefghi";
printf("This is the string: %s\n", str);
printf("This is the string after strnset: %s\n", strnset(str, 'x', 4));
printf("This is the string after strset: %s\n", strset(str, 'k'));
return 0;
/****************************************************************************
The output should be:
This is the string: abcdefghi
This is the string after strnset: xxxxefghi
This is the string after strset: kkkkkkkkk
****************************************************************************/
}
Related Information