Skip to content

How to perform split and join functions on Array in C?

An answer to this question on Stack Overflow.

Question

How can I perform join and split functions on array elements in C?

For example, let's say I have two arrays:

int value[]=  {0,1,2,3};
int id[]  = {1,1,3,3};
// I want to join the elements of "value" array whoz value of "id" are same.
//In my case 0,1 elements from "value" array has the same "id" value that is 1,1
//and 2,3 from "value" has same "id" value 3,3. So it will join {01,23}
//And if i want to perform split function it will give me back{0,1,2,3}

I did the same thing in perl script but i am not sure how can I get this functionality in C?

Answer

The following will do what you want:

#include <stdlib.h>
#include <stdio.h>
int main(){
  int i,v;
  int ID_SIZE=7;
  int value[]={0,1,2,3,1,4, 7};
  int id[]=   {1,1,3,3,2,2,10};
  //Discover largest and smallest ids in order to allocate memory
  int min=0,max=0,length;
  for(i=1;i<ID_SIZE;++i){
    if(id[i]<id[min]) min=i;
    if(id[i]>id[max]) max=i;
  }
  //Replace ids with values
  min=id[min];
  max=id[max];
  length=max-min+1;
  int **unions;
  int *append;
  append=(int *)calloc(length,sizeof(int));
  for(i=0;i<length;++i)
    append[i]=-1;    //Initial insertion point for each id is at 0
  //Create 2D array unions[IDS HERE][VALUES HERE]
  unions=(int **)calloc(length,sizeof(int*));
  for(i=0;i<length;++i)
    unions[i]=(int *)calloc(ID_SIZE,sizeof(int));
  //Join arrays
  for(i=0;i<ID_SIZE;++i){
    printf("Inserting %d into %d at %d\n",value[i],id[i],append[id[i]-min]+1);
    unions[id[i]-min][++append[id[i]-min]]=value[i];
  }
  for(i=0;i<length;++i){
    if(append[i]>=0){
      printf("Id %d has: ",i+min);
      for(v=0;v<=append[id[i]-min];++v)
        printf("%d ",unions[i][v]);
      printf("\n");
    }
  }
  return 0;
}

It creates two dynamic arrays.

One array, called append keeps track of how many values have been found for each id.

The other array, called unions stores the result of the computation.

In the case of the input I define in the program, the following is returned:

Inserting 0 into 1 at 0
Inserting 1 into 1 at 1
Inserting 2 into 3 at 0
Inserting 3 into 3 at 1
Inserting 1 into 2 at 0
Inserting 4 into 2 at 1
Inserting 7 into 10 at 0
Id 1 has: 0 1 
Id 2 has: 1 4 
Id 3 has: 2 3 
Id 10 has: 7