4

I am probably missing a subtlety for this question, as I couldn't find relevant results on the internet while I thought it would be a common one.

Anyway, here is my problem:

I have an array of strings, which I want to concatenate into a single string, from char** to char*.

Is there any function available for that purpose, or do I have to allocate memory for a new string, and use strcat() in a loop in order to concatenate all of the strings in array one by one?

Thanks in advance!

7
  • Does the number of strings in the array vary? Commented Mar 19, 2015 at 15:56
  • It does, but it's not a bother, as I have ways to know it. Commented Mar 19, 2015 at 16:00
  • 1
    It wouldn't make much of a difference anyway. glibc has an asprintf(3) function that allocates the target string for you that you could have used with asprintf(&concat, "%s%s%s", s[0], s[1], s[2]) to save a slight amount of work, but it's non-standard. :) Commented Mar 19, 2015 at 16:08
  • Can you stand it on its head? Start with a single array and slice it when you need to? Commented Mar 19, 2015 at 16:13
  • @Mawg, I didn't get the question? Commented Mar 19, 2015 at 16:21

1 Answer 1

4

There is no standard library function to do that.

The most efficient solution is to compute the size of the concatenated string, and then strcpy the components into the correct place. That requires keeping track of the sizes of the strings, which is a bit of overhead, or scanning the length of each component string twice.

Using strcat will require rescanning the constructed string on every concatenation, leading to quadratic running time. However, if you know you won't have too many strings in the array and the operation is not in a critical loop, it might be acceptable.


Here's a simple recursive solution, for demonstration purposes. Like all recursive programs, it should be used with caution in production code since it has the potential to overflow the call stack, but it can easily be modified to become an iterative solution.

(The standard iterative transformation would require a stack to retain the state of the recursive function, but the part of the state required after the recursive call consists only of my_len which could be recomputed.)

char* join_helper(char** in, size_t inlen, size_t inpos, size_t accum) {
  if (inpos == inlen)
    return strcpy(malloc(accum + 1) + accum, "");
  else {
    size_t mylen = strlen(in[inpos]);
    return memcpy(
      join_helper(in, inlen, inpos + 1, accum + mylen) - mylen,
      in[inpos], mylen);
  }
}

/* The string returned by this function is malloc'd and needs
 * to be free'd by the caller
 */
char* join(char** in, size_t inlen) {
  return join_helper(in, inlen, 0, 0);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Knowing the size won't be a problem, I'll have to see how strcpy works, though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.