w3resource

C Exercises: Concatenate two arrays


107. Concatenate Two Arrays

Write a program in C to concatenate two given arrays of integers.
If this is as simple as array1 + array2, so be it.

Sample Data:
({ 10, 20, 30, 40, 50, 60 }, { 70, 80, 90, 100, 110, 120 }) -> "10 20 30 40 50 60 70 80 90 100 110 120"

Sample Solution:

C Code:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

// Macro to concatenate arrays of any type
#define CONCAT_ARRAY(TYPE, A, An, B, Bn) \
  (TYPE *)concatenateArrays((const void *)(A), (An), (const void *)(B), (Bn), sizeof(TYPE));

// Function to concatenate arrays
void *concatenateArrays(const void *a, size_t an,
                        const void *b, size_t bn, size_t s)
{
  // Allocate memory for the concatenated array
  char *p = malloc(s * (an + bn));

  // Copy elements from first array to concatenated array
  memcpy(p, a, an * s);

  // Copy elements from second array to concatenated array after the first array's elements
  memcpy(p + an * s, b, bn * s);

  // Return pointer to the concatenated array
  return p;
}

// Sample arrays
const int x[] = { 10, 20, 30, 40, 50, 60 };
const int y[] = { 70, 80, 90, 100, 110, 120 };

int main(void)
{
  unsigned int i;

  // Concatenate arrays x and y
  int *z = CONCAT_ARRAY(int, x, 6, y, 6);

  // Display original arrays
  printf("Original arrays:\n");
  printf("Array-1:\n");
  for(int i = 0; i < 6; i++)
    printf("%d ", x[i]);
  printf("\nArray-2:\n");
  for(i = 0; i < 6; i++)
    printf("%d ", y[i]); 

  // Display concatenated array
  printf("\nConcatenate above arrays:\n");
  for(i = 0; i < 12; i++)
    printf("%d ", z[i]);

  free(z); // Free dynamically allocated memory for concatenated array
  return 0;
}

Output:

Original arrays:
Array-1:
10 20 30 40 50 60 
Array-2:
70 80 90 100 110 120 
Concatenate above arrays:
10 20 30 40 50 60 70 80 90 100 110 120 

Flowchart:/p> Flowchart: Double its value and replace the next number with 0 if current and next value are same and shift all 0's to the end.

For more Practice: Solve these Related Problems:

  • Write a C program to concatenate two arrays of integers using dynamic memory allocation.
  • Write a C program to merge two arrays and then sort the resulting concatenated array.
  • Write a C program to concatenate two arrays without using extra space by pre-allocating sufficient memory.
  • Write a C program to concatenate two arrays and then remove any duplicate elements from the merged array.

Go to:


PREV : Array Transformation: Double Value & Zero Shift.
NEXT : C Programming Structure Exercises Home

C Programming Code Editor:



Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.