求strtok函数的实现代码.

thorpe 2009-01-03 11:00:39
请大家帮忙回复一下strtok函数的实现代码.
...全文
789 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
yanxiaotao 2010-03-05
  • 打赏
  • 举报
回复
char *strtok_func(char *strToken, const char *strDelimit)
{
static char *pToken = NULL;
char *pchDelimit = NULL;
char *pchRetToken = NULL;
char ucFlag = 0;

if (strToken != NULL)
{
pToken = strToken;
}

if ((*pToken == '\0') || (pToken == NULL))
{
return NULL;
}

pchRetToken = pToken;
while (*pToken != '\0')
{
for (pchDelimit = strDelimit; *pchDelimit != '\0'; pchDelimit ++)
{
if (*pchRetToken == *pchDelimit)
{
pchRetToken ++;
break;
}

if (*pToken == *pchDelimit)
{
*pToken = '\0';
ucFlag = 1;
}
}
pToken ++;

if (ucFlag == 1)
{
break;
}
}

return pchRetToken;
}
zjf30366 2009-01-04
  • 打赏
  • 举报
回复
strtok用来分解字符串,使用后会将字符串破坏掉。
waizqfor 2009-01-03
  • 打赏
  • 举报
回复

char *strtok(register char *s,register const char *delim )
{
register char *spanp;
register int c, sc;
char *tok;
static char *last;


if (s == NULL && (s = last) == NULL)
return (NULL);

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;

for (; {
c = *s++;
spanp = (char *)delim;
do {
if ((sc = *spanp++) == c) {
if (c == 0)
s = NULL;
else
s[-1] = 0;
last = s;
return (tok);
}
} while (sc != 0);
}
}
星羽 2009-01-03
  • 打赏
  • 举报
回复
另外一个版本


/* Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details */
#include <string.h>

char* strtok(char *s, const char *delim)
{
const char *spanp;
int c, sc;
char *tok;
static char *last;


if (s == NULL && (s = last) == NULL)
return (NULL);

/*
* Skip (span) leading delimiters (s += strspn(s, delim), sort of).
*/
cont:
c = *s++;
for (spanp = delim; (sc = *spanp++) != 0;) {
if (c == sc)
goto cont;
}

if (c == 0) { /* no non-delimiter characters */
last = NULL;
return (NULL);
}
tok = s - 1;

/*
* Scan token (scan for delimiters: s += strcspn(s, delim), sort of).
* Note that delim must have one NUL; we stop if we see that, too.
*/
for (;;) {
c = *s++;
spanp = delim;
do {
if ((sc = *spanp++) == c) {
if (c == 0)
s = NULL;
else
s[-1] = 0;
last = s;
return (tok);
}
} while (sc != 0);
}
/* NOTREACHED */
}


星羽 2009-01-03
  • 打赏
  • 举报
回复
vs2005的


char * __cdecl strtok (
char * string,
const char * control
)
#endif /* _SECURE_VERSION */
{
unsigned char *str;
const unsigned char *ctrl = control;

unsigned char map[32];
int count;

#ifdef _SECURE_VERSION

/* validation section */
_VALIDATE_RETURN(context != NULL, EINVAL, NULL);
_VALIDATE_RETURN(string != NULL || *context != NULL, EINVAL, NULL);
_VALIDATE_RETURN(control != NULL, EINVAL, NULL);

/* no static storage is needed for the secure version */

#else /* _SECURE_VERSION */

_ptiddata ptd = _getptd();

#endif /* _SECURE_VERSION */

/* Clear control map */
for (count = 0; count < 32; count++)
map[count] = 0;

/* Set bits in delimiter table */
do {
map[*ctrl >> 3] |= (1 << (*ctrl & 7));
} while (*ctrl++);

/* Initialize str */

/* If string is NULL, set str to the saved
* pointer (i.e., continue breaking tokens out of the string
* from the last strtok call) */
if (string)
str = string;
else
str = _TOKEN;

/* Find beginning of token (skip over leading delimiters). Note that
* there is no token iff this loop sets str to point to the terminal
* null (*str == '\0') */
while ( (map[*str >> 3] & (1 << (*str & 7))) && *str )
str++;

string = str;

/* Find the end of the token. If it is not the end of the string,
* put a null there. */
for ( ; *str ; str++ )
if ( map[*str >> 3] & (1 << (*str & 7)) ) {
*str++ = '\0';
break;
}

/* Update nextoken (or the corresponding field in the per-thread data
* structure */
_TOKEN = str;

/* Determine if a token has been found. */
if ( string == str )
return NULL;
else
return string;
}

69,470

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧