Consider the code given below. Write a custom print function\"cprintf()\" that i
ID: 3614763 • Letter: C
Question
Consider the code given below. Write a custom print function"cprintf()" that implements a buffer. The function takes a singlestring as input and returns nothing. All bytes in the string arebuffered until the percent symbol (%) is encountered. Uponencountering that symbol, all contents of the buffer should be restto empty. The percent symbol should not be printed; it is only atrigger. The buffer only needs to be large enough for this example,do not worry about overflow. The newline character should beprinted but should not cause a flush. #include <stdio.h> #include <stdlib.h> #include <string.h> /* custom function cprintf() goes here */ main() { cprintf("Test "); sleep(1); cprintf(Re%test "); sleep(1); cprintf("All done %); } Consider the code given below. Write a custom print function"cprintf()" that implements a buffer. The function takes a singlestring as input and returns nothing. All bytes in the string arebuffered until the percent symbol (%) is encountered. Uponencountering that symbol, all contents of the buffer should be restto empty. The percent symbol should not be printed; it is only atrigger. The buffer only needs to be large enough for this example,do not worry about overflow. The newline character should beprinted but should not cause a flush. #include <stdio.h> #include <stdlib.h> #include <string.h> /* custom function cprintf() goes here */ main() { cprintf("Test "); sleep(1); cprintf(Re%test "); sleep(1); cprintf("All done %); }Explanation / Answer
//Hope this will help you.
#include<stdio.h>
#include <stdlib.h>
#include <string.h>
/* custom function cprintf()*/
void cprintf(char *p)
{
int i,len;
static char str[1000];
memset(str,0,sizeof(str));
strcpy(str,p);
len=strlen(str);
for(i=0;i<len;i++)
{
if(str[i]=='%'){
str[i]='';
break;
}
}
printf("%s ",str);
}
main()
{
cprintf("Test ");
sleep(1);
cprintf("Re%test ");
sleep(1);
cprintf("All done ");
system("pause");
}