C: Concatenate strings with dynamic memory.
https://github.com/pereiradaniel/c_programs/blob/master/strappend_dynmem.c // Implement string concatentation with dynamic memory #include <stdio.h> #include <string.h> // use strings #include <stdlib.h> // use calloc // returns ptr to new string on heap char *string_append(char* s1, char* s2) { // make space for new string on the heap that can store s1 and s2 // get lengths of s1 and s2 using strlen int s1l = strlen(s1); // Returns string length - null terminator int s2l = strlen(s2); int size = s1l + s2l + 1; // +1 for null terminator! // allocate space char *s = calloc(size, sizeof(char)); // copy chars from s1 into s for (int i =0; i < s1l; ++i) s[i] = s1[i]; // copy chars from s2 into s for (int i=0; i < s2l; ++i) s[s1l + i] = s2[i]; // s1l + i to shift over index! // insert null terminator s[size - 1] = '\0'; return s; } int main(int argc, char* argv[]) { ...