You can work around the code snippet below:
char szSpaces[] = " ";
char szStars[] = "*****************************************";
int iSpaces = 5;
int iStars = 7;
printf("%.*s%.*s\n", iSpaces, szSpaces, iStars, szStars);
Such a
format string instruct the
printf
function to print the first
iSpaces
characters of the
szSpaces
string, followed by the first
iStars
characters of the
szStars
string.
Note that if the number of required characters is greater than the string length, the full string is printed without any padding.
You can write your function with a simple loop, where on each step you compute the right number of spaces and asterisks to print.