How do I concatenate the string elements of my array into a single string in C? -
i have array of strings , create new string concatenation of array elements. appreciated, thanks
#include <stdio.h> #include <stdlib.h> #include <string.h> char *concatenate(size_t size, char *array[size], const char *joint){ size_t jlen, lens[size]; size_t i, total_size = (size-1) * (jlen=strlen(joint)) + 1; char *result, *p; for(i=0;i<size;++i){ total_size += (lens[i]=strlen(array[i])); } p = result = malloc(total_size); for(i=0;i<size;++i){ memcpy(p, array[i], lens[i]); p += lens[i]; if(i<size-1){ memcpy(p, joint, jlen); p += jlen; } } *p = '\0'; return result; } int main(){ char *ss[] = { "first", "second", "last" }; char *cat = concatenate(3, ss, ""); puts(cat); free(cat); cat = concatenate(3, ss, ", "); puts(cat); free(cat); return 0; }
Comments
Post a Comment