#include #include #include char *nconcat(const char **str) { int length; /* For keeping track of the length of the strings. */ char *rv; /* The return string. */ int i; /* First, calculate the length of the return string. I add one for each string, to account for the spaces between strings, and then for the final null character. */ length = 0; for (i = 0; str[i] != NULL; i++) { length += strlen(str[i]); length++; } /* Allocate the return string. */ rv = (char *) malloc(sizeof(char) * length); if (rv == NULL) { perror("malloc"); exit(1); } /* Build the string incrementally, using the length so that strcpy() always starts at the end of rv. */ strcpy(rv, ""); length = 0; for (i = 0; str[i] != NULL; i++) { if (i != 0) { strcpy(rv+length, " "); length++; } strcpy(rv+length, str[i]); length += strlen(str[i]); } return rv; } int main(int argc, const char **argv) { printf("%s\n", nconcat(argv)); }