网站建设资讯

NEWS

网站建设资讯

c语言字符串分割函数 c语言字符串

C语言有没有把字符串拆分为数组的函数?

用strtok函数实现吧。

创新互联建站专注于城区网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供城区营销型网站建设,城区网站制作、城区网页设计、城区网站官网定制、微信小程序服务,打造城区网络公司原创品牌,更为您提供城区网站排名全网营销落地服务。

void split( char **arr, char *str, const char *del)//字符分割函数的简单定义和实现

{

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[] = "张三$|男$|济南$|大专学历$|";

memset(myArray, 0x0, sizeof(myArray));

split(myArray, s, "$|");

for (i=0; i4; i++)

{

printf("%s\n", myArray[i]);

}

return 0;

}

C语言函数字符串截取分割

C标准库中提供了一个字符串分割函数strtok();

实现代码如下:

#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;

}

运行结果截图如下:

C语言中字符切割函数split的实现

#include stdio.h

#include string.h

// 将str字符以spl分割,存于dst中,并返回子字符串数量

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语言字符串分割函数 c语言字符串
URL分享:http://cdweb.net/article/hgppdd.html