How to ensure the string is full of spaces for a given length?
step 1:
char buf[MAX_SIZE];
sprintf(buf,"%*s",MAX_SIZE-1,""); //fill buffer with spaces
step 2:
Now use strncmp()
compare strct.len number of character of strct.data
character array with buf
if(strncmp(strct.data ,buf ,strct.len) ==0)
{
//all are spaces
}
You need not to repeat step1.
Another solution jxh suggested you can also use memset()
instead of sprintf()
memset(buf, ' ', sizeof buf); //fill buf with all spaces
You need do this once, next time onwards you need not do this.
You can also use VLA.
declaring char buf[strct.len]
;
but you need to use memset each time.