#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 1024x768 and you can modify it
Char *new;
Char buf[1024] = {'} '};
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, ' n ', 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, ' n ', sizeof (BUF));
printf ("arry[%d] =%s\n", J, Arry[j]);
J + +;
memset (buf, ' n ', sizeof (BUF));
}
}
New = (char *) malloc (1);//ened indication in the str arry
New[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;
}
Use C to implement a string split and return all substrings