#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int stringCount(char *str, char des)
{
int count = 0;
char *p = NULL;
p = str;
while(*p != '\0')
{
if(*p == des)
count ++;
p++;
}
return count;
}
void stringCut(char *str, char *des)
{
int i=0;
char *p = NULL;
p = str;
if(*p == ',')
{
p++;
}
while(*p != '\0')
{
if(*p == ',')
break;
des[i] = *p;
i++;
p++;
}
}
int main ()
{
int i = 0, count = 0;
char *a = "sfa,ad,dfe,adfe,dwd";
char *bb=NULL;
char *c[32];
memset(c, 0, sizeof(c));
count = stringCount(a, ',');
for(i = 0; i<count + 1 && a!='\0'; i++)
{
c[i] = (char *)malloc(sizeof(char)*64);
memset(c[i], 0, sizeof(char)*64);
stringCut(a, c[i]);
printf("c[%d]:%s\n",i, c[i]);
bb = strstr(a,",");
bb++;
a=bb;
}
for(i = 0;i<5;i++)
{
free(c[i]);
}
return 0;
}
sfa,ad,dfe,adfe,dwd
输出:c[0]:sfa
c[1]:ad
c[2]:dfe
c[3]:adfe
c[4]:dwd