C Program to Concatenate Two Arrays
In this c programming example we will learn how to concatenate two arrays
Algorithm
- Take an two array of integers by scanf() function
- Assign values to two different variables a and b
- Take other variable c with size as a & b
- Iterate first array and add items into array c from array a
- Similarly iterate array b and add items into array c
- Now array c contains combination of array a & b
- Print the sum of array at the end of the loop
Simple C example to concatenate two arrays
#include <stdio.h>
int main() {
int c[10];
int a[5] = {1,3,5,6,8};
int b[5] = {99,78,23,34,12};
int loop, index, len;
len= 5;
index = 0;
for(loop = 0; loop < len; loop++) {
c[index] = a[loop];
index++;
}
for(loop = 0; loop < len; loop++) {
c[index] = b[loop];
index++;
}
printf("\nEven -> ");
for(loop = 0; loop < len; loop++)
printf(" %d", a[loop]);
printf("\nOdd -> ");
for(loop = 0; loop < len; loop++)
printf(" %d", b[loop]);
printf("\nConcat -> ");
for(loop = 0; loop < 10; loop++)
printf(" %d", c[loop]);
return 0;
}
|
Output
Even -> 1 3 5 6 8
Odd -> 99 78 23 34 12
Concat -> 1 3 5 6 8 99 78 23 34 12
|