3

I have two strings char str1[41] e char str2[41]. If I use strcat function to concatenate them I get an unique string without space instead I would have space between these.

this could be a way:

strcat(strcat(str1, " "),str2)

Does it exist another way?

Mazzy
  • 13,354
  • 43
  • 126
  • 207

6 Answers6

8

sprintf(destination, "%s %s", str1, str2);

You'll have to provide a destination. You could use str1 as your destination and it might work correctly, but note the way you're currently doing things could cause you to overrun str1s buffer.

asprintf() will allocate a buffer for you, of the correct (read: safe) length, but you must remember to free() that buffer when you're done with it.

mah
  • 39,056
  • 9
  • 76
  • 93
  • `asprintf()` is great but be aware that it's not POSIX standard and thus not available everywhere – Ingo Leonhardt Aug 08 '13 at 11:51
  • 1
    http://www.mibsoftware.com/libmibx/parse/asprintf.c appears to be a liberally licensed version of it in source form. I've not used that implementation myself. – mah Aug 08 '13 at 11:53
2

If strcat is a requirement, I believe that the most efficient way would be:

size_t length = strlen(str1);
str1[length] = ' '; // overwrite null termination
str1[length+1] = '\0'; // add a new null termination
strcat(str1, str2);

And of course, be aware of buffer overflows.

Lundin
  • 195,001
  • 40
  • 254
  • 396
  • 2
    even more efficient (if `strcat()` isn't required): `strcpy( str1+length+1, str2 )` instead of `str1[length+1] = '\0'` and `strcat(str1, str2);` – Ingo Leonhardt Aug 08 '13 at 11:30
  • @IngoLeonhardt Yes, added clarification. If strcat isn't required, then `memcpy(str1+strlen(str1), str2, strlen(str2)+1);` would be far more efficient. – Lundin Aug 08 '13 at 11:32
  • 1
    @Lundin I don't think memcpy would be more efficient because of the need to determine strlen(str2). That would cause you to walk through that string twice, while a simple strcat walks through it only once, stopping at the NULL. – mah Aug 08 '13 at 11:34
  • @mah strcat will both check each character for null and copy it, so it executes roughly twice the amount of instructions as memcpy. In theory, memcpy+strlen together should be pretty much the same amount of instructions as strcat. I suppose it isn't obvious which would be the fastest, it depends on how heavily memcpy is optmized, cache memory etc etc. – Lundin Aug 08 '13 at 11:44
1

Placing curly brackets around the space solves the problem: strcat(str, {' '})

ryanpattison
  • 6,151
  • 1
  • 21
  • 28
codekitty
  • 1,438
  • 3
  • 20
  • 30
0

Add a whitespace at the end of str1 before concatenation with str2 , or add a whitespace at the beginning of str2 and then concatenate the two strings...

0

To maintain efficiency and respect the limited array size of str1,

size_t Len1 = strlen(str1);
size_t Len2 = strlen(str2);
if ((Len1 + 1 + Len2) >= sizeof(str1)) {
  ; // handle error
}
str1[Len1] = ' ';
memcpy(&Str1[Len1+1], str2, Len2 + 1);

OR

as defined in C11 K.3.7.1.3., if available, use strcpy_s()

size_t Len = strlen(str1);
strcpy_s(&str1[Len], Len - sizeof(str1), " ") || strcpy_s(&str1[Len+1], Len - (sizeof(str1) + 1), str2);

Premise: strcat() is not required.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
0

You can try this. I hope it will help you.

strcat(string1, " ");
strcat(string1, string2);
anaszaman
  • 297
  • 6
  • 19