关于c语言字符串拆分函数的信息

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语言有没有把字符串拆分为数组的函数?

用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语言中字符切割函数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语言字符串拆分函数的信息
本文路径:https://www.cdcxhl.com/article32/hjgssc.html

成都网站建设公司_创新互联,为您提供自适应网站网站排名关键词优化网站设计公司用户体验建站公司

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

网站优化排名