strtok的实现——用于按给定符号分割字符串的函数
来源:互联网 发布:php显示图片缩略图 编辑:程序博客网 时间:2024/06/05 10:24
char *mystrtok(char *s,const char *delim)
{
static char *last;
char *tok;
char *ucdelim;
char *spanp;
int c,sc;
/**//*s为空,并且上次剩余值也为空,则直接返回NULL,否则s为last或当前值中有值的一方*/
if (s == NULL && (s = last) == NULL)
return NULL;
int found = 0;//是否找到与delim匹配的字符
//处理连续的待匹配的字符
cont:
c=*s++;
for (spanp = (char *)delim;(sc = *spanp++) != 0;)
{
if (c == sc)
goto cont;
}
if (c == 0)
{
last = NULL;
return NULL;
}
tok = s-1;
while (!found && *s != '\0')
{
ucdelim = (char *) delim;
while (*ucdelim)
{
if (*s == *ucdelim)
{
found = 1;
*s = '\0';
last = s + 1;
break;
}
ucdelim++;
}
if (!found)
{
s++;
if(*s=='\0')
last = NULL;
}
}
return tok;
{
static char *last;
char *tok;
char *ucdelim;
char *spanp;
int c,sc;
/**//*s为空,并且上次剩余值也为空,则直接返回NULL,否则s为last或当前值中有值的一方*/
if (s == NULL && (s = last) == NULL)
return NULL;
int found = 0;//是否找到与delim匹配的字符
//处理连续的待匹配的字符
cont:
c=*s++;
for (spanp = (char *)delim;(sc = *spanp++) != 0;)
{
if (c == sc)
goto cont;
}
if (c == 0)
{
last = NULL;
return NULL;
}
tok = s-1;
while (!found && *s != '\0')
{
ucdelim = (char *) delim;
while (*ucdelim)
{
if (*s == *ucdelim)
{
found = 1;
*s = '\0';
last = s + 1;
break;
}
ucdelim++;
}
if (!found)
{
s++;
if(*s=='\0')
last = NULL;
}
}
return tok;
}
int main(void)
{
char *ptr;
char buffer[] = {"12121321 dasdasdas-dsadasd"};
char *delims = "- ";
ptr = mystrtok(buffer, delims);
printf("%d\n", strlen(ptr));
while(ptr != NULL)
{
printf("%s\n", ptr);
ptr = mystrtok(NULL, delims);
}
printf("%s\n", buffer);
exit(0);
}
0 0
- strtok的实现——用于按给定符号分割字符串的函数
- 字符串分割函数strtok的实现
- VC实现字符串分割的函数strtok
- strtok()—字符串分割函数
- strtok() 用于分割字符串
- 用于分隔字符串的strtok()函数
- strtok函数实现字符串分割
- 字符串分割函数strtok的用法
- strtok函数的妙用,分割字符串
- strtok函数,分割字符串
- strtok 字符串分割函数
- 字符串分割函数 strtok
- 分割字符串:strtok()函数
- strtok函数分割字符串
- 分割字符串:strtok()函数
- strtok函数分割字符串
- strtok字符串分割函数
- strtok:字符串分割函数
- 数据库开发规范
- unity遮挡剔除的开启
- 快速排序
- 微信公众号开发
- Android 多线程handler,runnable,asyctask使用
- strtok的实现——用于按给定符号分割字符串的函数
- 【OpenCV】大牛博客
- 二叉树的创建、先序、中序、后序、层序的递归与非递归算法(java)
- javaString常用方法
- java 网络爬虫之多线程抓取文件
- Android synchronized()同步线程
- Unity琐碎(3) UGUI 图文混排解决方案和优化
- Python标准库05 存储对象 (pickle包,cPickle包)
- 工作常用的git指令