Question

Write a function that takes an array of C-strings as a parameter and the number of...

Write a function that takes an array of C-strings as a parameter and the number of elements currently stored in the array, and returns a single C-string pointer that is the concatenation of all C-strings in the array.

Note: The function must take 2 parameters and return "char *". Any other ways will cause some deduction of points.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
char* concatenation(char* arr[], int n){
   int i, newSize = 1, j, k = 0;
   char* newArr;
   for(i = 0;i<n;i++){
      newSize += strlen(arr[i]);
   }
   newArr = (char*)malloc(sizeof(newSize));
   for(i = 0;i<n;i++){
      for(j = 0;j<strlen(arr[i]);j++){
         newArr[k++] = arr[i][j];
      }
   }
   newArr[newSize-1] = '\0';
   return newArr;
}

Add a comment
Know the answer?
Add Answer to:
Write a function that takes an array of C-strings as a parameter and the number of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT