- 论坛徽章:
- 4
|
贴一个C实现的函数:
调用示例:
#define MAX_COLS 10
char src[256];
char *cols[MAX_COLS];
int count;
int i;
strcpy(src, "aa aa bbc cccd");
count = splitEx(src, ' ', cols, MAX_COLS);
for (i=0; i<count; i++)
{
printf("%d. %s\n", i+1, cols[i]);
}
int splitEx(char *src, const char seperator, char **pCols, const int nMaxCols)
{
char *p;
char **pCurrent;
int count = 0;
if (nMaxCols <= 0)
{
return 0;
}
p = src;
pCurrent = pCols;
while (true)
{
*pCurrent = p;
pCurrent++;
count++;
if (count >= nMaxCols)
{
break;
}
p = strchr(p, seperator);
if (p == NULL)
{
break;
}
*p = '\0';
p++;
}
return count;
} |
|