c语言中分隔函数 c语言语句分隔符

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; icnt; i++)
puts(dst[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语言有没有把字符串拆分为数组的函数?用strtok函数实现吧 。
【c语言中分隔函数 c语言语句分隔符】void split( char **arr, char *str, const char *del)//字符分割函数c语言中分隔函数的简单定义和实现
{
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语言语句分隔符的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站 。

    推荐阅读