14

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?

SomethingSomething
  • 11,491
  • 17
  • 68
  • 126
  • 5
    Have you looked at memset? I just ask you to take care with buffer overflows. – oblitum Feb 18 '14 at 13:53
  • Take a look [here](http://stackoverflow.com/questions/2488563/strcat-implementation). All tries can convert to straightforward algorithms and you can boost the them performance with assumptions and optimizations. The main issue is too grant `dest` enough space to receive new characters. – wesley.mesquita Feb 18 '14 at 14:00
  • I assume that there is enough spaces for the characters in the destination. The question is whether there is a standard way to generate such string of spaces in one command – SomethingSomething Feb 18 '14 at 14:05
  • 1
    As others pointed out, I believe `memset` is the answer. – wesley.mesquita Feb 18 '14 at 14:10
  • 1
    @user3322273 you first init your `dest` array by `memset` like `memset( dest, ' ', sizeof(dest));`. So after that you not even need to add space or your implementation also optimized. – Jayesh Bhoi Feb 18 '14 at 14:13

3 Answers3

10

I would do

add_spaces(char *dest, int num_of_spaces) {
    int len = strlen(dest);
    memset( dest+len, ' ', num_of_spaces );   
    dest[len + num_of_spaces] = '\0';
}

But as @self stated, a function that also gets the maximum size of dest (including the '\0' in that example) is safer:

add_spaces(char *dest, int size, int num_of_spaces) {
    int len = strlen(dest);
    // for the check i still assume dest tto contain a valid '\0' terminated string, so len will be smaller than size
    if( len + num_of_spaces >= size ) {
        num_of_spaces = size - len - 1;
    }  
    memset( dest+len, ' ', num_of_spaces );   
    dest[len + num_of_spaces] = '\0';
}
Ingo Leonhardt
  • 9,435
  • 2
  • 24
  • 33
  • Unsafe, total size of dest is missing. – this Feb 18 '14 at 13:53
  • 2
    @self any version of `add_spaces()` that only takes these two arguments depends on the caller to check for appropriate sizes. I will add a solutiion that also taks the maximum size. – Ingo Leonhardt Feb 18 '14 at 13:55
4
void add_spaces(char *dest, int num_of_spaces) {
    sprintf(dest, "%s%*s", dest, num_of_spaces, "");
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
1

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

So in main suppose you declared your array like char dest[100] then initialize your string with speces.

like

char dest[100];
memset( dest,' ',sizeof(dest)); 

So you not need even add_spaces(char *dest, int num_of_spaces).

Jayesh Bhoi
  • 24,694
  • 15
  • 58
  • 73
  • A `0` set after the spaces would be nice. Also, what does your answer bring that isn't in the answer from @Ingo above, posted 28 min before yours? – Déjà vu Feb 18 '14 at 14:25