之前分享了一篇使用C++(std::string 作为容器)进行字符串分割的博文:
https://blog.csdn.net/r5014/article/details/82802664
现在又想用C语言做一个字符串分割的函数,大概功能是这样:
需要分割的字符串“ this is a charactor raw. ” 使用 ' '分割
分割之后会返回一个char** strv 表示分割出子串str的vector,返回一个int strc表示分割出子串的数量,在使用完毕之后自行释放strv
strv可能是NULL 比如" "使用‘ ’分割之后就是NULL。
以下介绍分割函数splitstr_c()
//* 切割字符串,strv返回字符串数组,strc返回分割之后的字符串数量
void splitstr_c(char* str, char c, char*** strv, int* strc)
{
int i = 0;
int j = 0;
int n = 0;
int offset_strv = 0;
int offset_font = 0;
int offset_back = 0;
int str_size = strlen(str);
char** tstrv = NULL;
for(i = 0; i < str_size; i++)
{
if(i > 0)
{
if((str[i] !