Try
printf ( "%*c\"%s\"%*c", leading, ' ', str, trailing, ' ');
Where leading and trailing are int.
For trailing use only
printf ( "\"%s\"%*c", str, trailing, ' ');
A similar modification can be made only for leading
EDIT
The width specifier allows you to use an asterisk to provide a variable width.
In this case,% * c tells printf to get the next argument and use it as the width for the character.
It initially reads the lead and uses it for the width of the field in which the character is printed, the space character. Then the quote \ "is printed. The format% s prints the next argument, str. Then another quote is printed, and then again% * c reads the final width for the field in which the next argument will be printed, another space character.
source share