注:部分函数依赖于
https://zh.arslanbar.net/post.php?t=23426中定义的一些函数
char *join(char delimiter, char **array)
{
int totallen = strarrtotallen(array);
int alen = strarrlen(array);
int len = totallen + alen;
if (len == 0)
len++;
char *str = (char *)malloc(len * sizeof(char));
char *pStr = str;
if (len == 1)
{
*str = '\0';
return str;
}
while (*array != NULL)
{
while (**array != '\0')
{
*pStr = **array;
pStr++;
(*array)++;
}
*pStr = delimiter;
pStr++;
array++;
}
*(pStr - 1) = '\0';
return str;
}
int main()
{
char *strarr[] = {"apple", "banana", "orange", NULL};
char *str = join(',', strarr);
puts(str);
free(str);
return 0;
}
输出:apple,banana,orange