當前位置:首頁 » 編程語言 » 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;
}

熱點內容
java返回this 發布:2025-10-20 08:28:16 瀏覽:559
製作腳本網站 發布:2025-10-20 08:17:34 瀏覽:841
python中的init方法 發布:2025-10-20 08:17:33 瀏覽:546
圖案密碼什麼意思 發布:2025-10-20 08:16:56 瀏覽:722
怎麼清理微信視頻緩存 發布:2025-10-20 08:12:37 瀏覽:645
c語言編譯器怎麼看執行過程 發布:2025-10-20 08:00:32 瀏覽:963
郵箱如何填寫發信伺服器 發布:2025-10-20 07:45:27 瀏覽:218
shell腳本入門案例 發布:2025-10-20 07:44:45 瀏覽:76
怎麼上傳照片瀏覽上傳 發布:2025-10-20 07:44:03 瀏覽:767
python股票數據獲取 發布:2025-10-20 07:39:44 瀏覽:673