以參數ch字符分割,分割char數組到char二維數組中,返回詞的個數
可處理多個重複ch的情況,無論開頭,中間,還是結尾
int mysplit(char *pstr, char(*pcutcmd)[10], char ch)
{
int ret = 0;
if (NULL != pstr && NULL != pcutcmd)
{
char *tempstr = pstr;
int i = 0, j = 0;//i爲行,j爲列每行爲一個字符串
int countrow = 0;
while (*tempstr)
{
if (ch == *tempstr)//遇到空格,詞語結束,加\0
{
*(*(pcutcmd + i) + j) = '\0';
}
else if ((ch == *(tempstr - 1)) && (ch != *tempstr))//只有在上一個是空格,這一個不是時數組行 i+1,j=0
{
if (countrow != 0)
i++;
j = 0;
*(*(pcutcmd + i) + j) = *tempstr;
j++;
}
else //非空格寫入字符
{
if(0==i)
{
countrow=1;
}
*(*(pcutcmd + i) + j) = *tempstr;
j++;
}
tempstr++;
}
ret = i;
}
return ret;
}
例如
“;
char arrstr[10][255]={0};
mysplit(str,arrstr,’ ‘);
結果:
arr[0]=”1”
arr[1]=”2”
arr[2]=”33”