當前位置:首頁 » 編程語言 » C語言split

C語言split

發布時間: 2024-04-30 07:52:44

⑴ C語言 寫一個把一個字元串分成若干個數組

void*Split(constchar*pString,intlength)
{
char*ptr=NULL;
introws;//一組等寬字元串可以看做二維數組的一行,定義行數
constchar*pSrc=pString;//取原地址作為源指針
char*pTag;//目標指針
//分割長度小於等於0,或指針無效時,返回空指針。
if(pString&&length>0)
{
intlen=strlen(pString);
intcols;//列數
rows=len/length;//字元串總長除以列數
if(len%length>0)rows++;//如果余數非0,則行數加一
ptr=newchar[rows*(length+1)];//創建足夠的空間
pTag=ptr;//初始化目標指針
while(*pSrc!='')//源指針指向的值如果有效則循環
{
cols=strlen(pSrc);//取原指針開始的字元串長度
if(cols>length)cols=length;//如果大於分割長度則修正,否則即為余數,就是最後一行的列數
memcpy(pTag,pSrc,cols);//復制
pTag+=length;//目標指針遞增一個列寬(行寬度)
*pTag='';//填寫結束符
pTag++;//增補一個地址
pSrc+=cols;//源指針遞增一個列寬(行寬度)
}
}
returnptr;
}

//主函數
int_tmain(intargc,_TCHAR*argv[])
{
char*str="abcdefghijklmn";
char(*p)[4];
p=(char(*)[4])Split(str,3);
cout<<p[1]<<endl;
getchar();
return0;
}

⑵ C語言中字元切割函數split的實現

#include<stdio.h>
#include<string.h>

//將str字元以spl分割,存於dst中,並返回子字元串數量
intsplit(chardst[][80],char*str,constchar*spl)
{
intn=0;
char*result=NULL;
result=strtok(str,spl);
while(result!=NULL)
{
strcpy(dst[n++],result);
result=strtok(NULL,spl);
}
returnn;
}

intmain()
{
charstr[]="whatisyouname?";
chardst[10][80];
intcnt=split(dst,str,"");
for(inti=0;i<cnt;i++)
puts(dst[i]);
return0;
}

熱點內容
怎麼知道支付寶密碼 發布:2025-09-17 07:12:37 瀏覽:422
壓縮性判斷句 發布:2025-09-17 07:11:44 瀏覽:140
php金額格式化 發布:2025-09-17 06:47:11 瀏覽:38
什麼是工作站伺服器 發布:2025-09-17 06:45:03 瀏覽:188
d盤無法訪問參數不正確 發布:2025-09-17 06:30:36 瀏覽:470
為什麼徵兵網無法訪問 發布:2025-09-17 06:19:31 瀏覽:376
mysqlsql語句變數賦值 發布:2025-09-17 06:19:26 瀏覽:37
真我3i什麼配置 發布:2025-09-17 06:17:59 瀏覽:141
輸入有效的伺服器地址ip 發布:2025-09-17 06:17:26 瀏覽:440
德育源碼 發布:2025-09-17 06:16:00 瀏覽:106