新聞中心
C語(yǔ)言有沒(méi)有把字符串拆分為數(shù)組的函數(shù)?
用strtok函數(shù)實(shí)現(xiàn)吧。
創(chuàng)新互聯(lián)建站專(zhuān)注于城區(qū)網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠(chéng)為您提供城區(qū)營(yíng)銷(xiāo)型網(wǎng)站建設(shè),城區(qū)網(wǎng)站制作、城區(qū)網(wǎng)頁(yè)設(shè)計(jì)、城區(qū)網(wǎng)站官網(wǎng)定制、微信小程序服務(wù),打造城區(qū)網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供城區(qū)網(wǎng)站排名全網(wǎng)營(yíng)銷(xiāo)落地服務(wù)。
void split( char **arr, char *str, const char *del)//字符分割函數(shù)的簡(jiǎn)單定義和實(shí)現(xiàn)
{
char *s =NULL;
s=strtok(str,del);
while(s != NULL)
{
*arr++ = s;
s = strtok(NULL,del);
}
}
int main()
{
int i;
char *myArray[4];
char s[] = "張三$|男$|濟(jì)南$|大專(zhuān)學(xué)歷$|";
memset(myArray, 0x0, sizeof(myArray));
split(myArray, s, "$|");
for (i=0; i4; i++)
{
printf("%s\n", myArray[i]);
}
return 0;
}
C語(yǔ)言函數(shù)字符串截取分割
C標(biāo)準(zhǔn)庫(kù)中提供了一個(gè)字符串分割函數(shù)strtok();
實(shí)現(xiàn)代碼如下:
#include?stdio.h
#include?string.h
#define?MAXSIZE?1024
int?main(int?argc,?char?*?argv[])
{
char?dates[MAXSIZE]?=?"$GPGGA,045950.00,A,3958.46258,N,11620.55662,E,0.115,,070511,,,A*76?";
char?*delim?=?",";
char?*p;
printf("%s??",strtok(dates,delim));
while(p?=?strtok(NULL,delim))
{
printf("%s??",p);
}
printf("\n");
return?0;
}
運(yùn)行結(jié)果截圖如下:
C語(yǔ)言中字符切割函數(shù)split的實(shí)現(xiàn)
#include?stdio.h
#include?string.h
//?將str字符以spl分割,存于dst中,并返回子字符串?dāng)?shù)量
int?split(char?dst[][80],?char*?str,?const?char*?spl)
{
int?n?=?0;
char?*result?=?NULL;
result?=?strtok(str,?spl);
while(?result?!=?NULL?)
{
strcpy(dst[n++],?result);
result?=?strtok(NULL,?spl);
}
return?n;
}
int?main()
{
char?str[]?=?"what?is?you?name?";
char?dst[10][80];
int?cnt?=?split(dst,?str,?"?");
for?(int?i?=?0;?i??cnt;?i++)
puts(dst[i]);
return?0;
}
文章題目:c語(yǔ)言字符串分割函數(shù) c語(yǔ)言字符串
瀏覽路徑:http://fisionsoft.com.cn/article/hgppdd.html