How to use this function

void test()
{
char sdest[20][50];
char source[50];
char ret;
sprintf(source,"This string is splitted");
ret=Split(source,sdest,' ');
return;
}
/*
Result:
sdest[0]="This"
sdest[1]="string"
sdest[2]="is"
sdest[3]="splitted"
ret=4
*/

Split Function

/*
input:  Mess, String how to split
input:  ch, specific character
output: dest, array of string
return: number of items
*/
char Split(char *Mess,char dest[20][50],char ch)//dest
{

char cret,j,m,inc;
char Msg2[50],i,len,row;
char temps[50],add;

strcpy(temps,Mess);
len=strlen(temps);
inc=0;
row=0;
m=0;
j=0;
cret=0;
add=0;

for(i=0;i<len;i++) { if(temps[i]!=ch) { Msg2[m++]=temps[i];//copy string inc=0; } else { if (!inc) add=1; } if((len-i)==1) add=1; if(add)//add item of string { inc=1; add=0; if(m>0)
{
Msg2[m]=0;
strcpy(dest[j++],Msg2);
}
cret=j;
m=0;
}
}
return cret;

}
Advertisements