I want to add a variable number of spaces to a string in C, and wanted to know if there is a standard way to do it, before I implement it by myself.
Until now I used some ugly ways to do it:
- Please assume that before I called any of the below functions, I took care to have enough memory allocated for the spaces I want to concatenate
This is one way I used:
add_spaces(char *dest, int num_of_spaces) {
int i;
for (i = 0 ; i < num_of_spaces ; i++) {
strcat(dest, " ");
}
}
This one is a better in performance, but also doesn't look standard:
add_spaces(char *dest, int num_of_spaces) {
int i;
int len = strlen(dest);
for (i = 0 ; i < num_of_spaces ; i++) {
dest[len + i] = ' ';
}
dest[len + num_of_spaces] = '\0';
}
So, do you have any standard solution for me, so I don't reinvent the wheel?