Use C to separate strings and return all substrings. Use c to separate strings.
# Include <stdio. h>
# Include <stdlib. h>
# Include <unistd. h>
# Include <string. h>
Char ** split (char * mother, char split_char)
{
Char * arry [1024]; // the MAX sub string is 1024 and you can modify it
Char * new;
Char buf [1024] = {'\ 0 '};
Int len, I, j, k, len_sub;
For (I = 0, j = 0, k = 0; I <strlen (mother); I ++) // scan the string "mother" to split by "split_char"
{
If (mother [I] = split_char)
{
Len_sub = strlen (buf );
Printf ("sub len is % d \ n", len_sub );
If (len_sub> 0)
{
Printf ("buf str is % s \ n", buf );
New = (char *) malloc (len_sub );
Strcpy (new, buf );
Arry [j] = new;
Printf ("arry [% d] = % s \ n", j, arry [j]);
J ++;
Memset (buf, '\ 0', sizeof (buf ));
K = 0;
Continue;
} Else {
Continue;
}
}
If (mother [I]! = Split_char)
{
Buf [k] = mother [I];
K ++;
}
If (strlen (mother)-1) = I & strlen (buf )! = 0)
{
Printf ("the last str is made \ n ");
Printf ("buf str is % s \ n", buf );
Len_sub = strlen (buf );
Printf ("sub len is % d \ n", len_sub );
New = (char *) malloc (len_sub );
If (new = NULL)
{
Printf ("malloc error \ n ");
}
Strcpy (new, buf );
Printf ("j = % d \ n", j );
Arry [j] = new;
Memset (buf, '\ 0', sizeof (buf ));
Printf ("arry [% d] = % s \ n", j, arry [j]);
J ++;
Memset (buf, '\ 0', sizeof (buf ));
}
}
New = (char *) malloc (1); // ened indication in the str arry
New [0] = '\ 0 ';
Arry [j] = new;
I = 0;
Printf ("in split func print result as below: \ n ");
While (strlen (arry [I])
{
Printf ("% s \ n", arry [I]);
I ++;
}
Printf ("ened print \ n ");
Return arry;
}
Int main (void)
{
Char * s = "abc def ghi jkl mno ";
Char ** ss;
Int I;
Ss = split (s ,'');
I = 0;
Printf ("in main func print result as below: \ n ");
While (strlen (ss [I])
{
Printf ("% s \ n", ss [I]);
I ++;
}
Printf ("ened print \ n ");
Return 0;
}