Logo

Programming-Idioms

History of Idiom 53 > diff from v43 to v44

Edit summary for version 44 by Dodopod:
New C implementation by user [Dodopod]

Version 43

2016-12-22, 09:27:39

Version 44

2017-05-27, 17:13:05

Idiom #53 Join a list of strings

Concatenate elements of string list x joined by the separator ", " to create a single string y.

Illustration

Idiom #53 Join a list of strings

Concatenate elements of string list x joined by the separator ", " to create a single string y.

Illustration
Imports
#include <string.h>
Code
#define DELIM ", "
#define L 64

char y[L] = {'\0'};

for (int i = 0; i < N; ++i)
{
    if (i && x[i][0])
        strcat(y, DELIM);
    
    strcat(y, x[i]);
}
Comments bubble
x is assumed to be an array containing N null-terminated strings.

L is arbitrary, but big enough to hold the concatenated string.