char* join(char** strings, size_t num_strings, char* seperator)
{
int cache = 0;
int count = 0;
for(int i = 0; i < num_strings; i++){
cache += strlen(strings[i]);
}
char * joined = malloc((cache + num_strings * strlen(seperator) + 1) * sizeof(char));
for(int j = 0; j < num_strings; j++){ //big loop
if(j != 0){
int j = 0;
while(j < strlen(seperator)){ //inserting seperators
joined[count] = seperator[j];
count++;
j++;
}
}
int insert_string = 0;
while( insert_string < strlen(strings[j])){ //inserting strings
joined[count] = strings[j][insert_string];
insert_string++;
count++;
}
}
joined[count] = 0; //Zero Termination
return joined;
}
I wrote this join() function, which creates a string from num_strings with specified separator, for example *. Is there a way to make this more concise? Also is the termination correct?
strcatandmemcpy? \$\endgroup\$