题目如下:
代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int spitStr(const char *inbuf, char c, char **outbuf, int *count)
{
outbuf = (char **)malloc(sizeof(char*) * 10); //分配内存
for (int i = 0; i < 10; ++i)
{
outbuf[i] = (char *)malloc(sizeof(char) * 50);
}
if (outbuf == NULL)
{
return -1;
}
//分离字符
for (int i = 0,j=0,k=0; inbuf[i] != '\0'; i++,k++)
{
if (inbuf[i] != c)
{
outbuf[j][k] = inbuf[i];
}
else
{
outbuf[j][k] = '\0';
j++;
k = -1;
(*count)++;
}
}
for (int i = 0; i < *count; i++)
{
printf("%s \n", outbuf[i]);
}
return 0;
}
void freeMem(char **p)
{
for (int i = 0; i < 10; ++i)
{
if (p[i] != NULL)
{
free(p[i]);
p[i] = NULL;
}
}
if (p != NULL)
{
free(p);
p = NULL;
}
}
int main()
{
int ret = 0;
int count = 0;
char *s1 = "asdasd,wgeasd,wagawd,asgfas,asfgwd,asudwsd,";
char **p1 = NULL;
ret = spitStr(s1, ',', p1, &count);
free(p1);
printf("line:%d\n", count);
if (ret != 0)
{
printf("splitError!!!\n");
}
system("pause");
return 0;
}