The ones who are crazy enough to think they can change the world are the ones who do.- Steve Jobs
memset() is one of the inbuilt string function in c programming which is used to replace first n characters of a string str with a character chr.
The following diagram clearly illustrate the working principle of memset() inbuilt string function in C.
In the above diagram memset() takes three parameter say str, chr and n. First n characters of a string str will be replaced by a character chr.
memset(str, setchar, n);
The above program defines the function memset(), which replace n characters of a string str.
#include <stdio.h> #include<string.h> int main() { char str[30] = "what the hell"; memset(str,'*',4); puts(str); return 0; }
**** the hell
The above program defines the function memset(), which replace n characters of a string str.
Let us replace n character of a string str without using any inbuilt string function.
#include <stdio.h> #include<string.h> int main() { char str[30] = "what the hell"; char chr = '*', i, num = 4; for(i=0; str[i]!='\0'; i++) { str[i] = chr; if(i == num-1) break; } puts(str); return 0; }
**** the hell
The above program looks verbose but yields the same result.
We may make mistakes(spelling, program bug, typing mistake and etc.), So we have this container to collect mistakes. We highly respect your findings.
© Copyright 2019