在线给分,请详细分析strtok()函数的用法??
请详细分析strtok()的用法,或说一说函数的实现? 问题点数:20、回复次数:14Top
1 楼rockhard(不签名)回复于 2002-07-31 17:28:35 得分 3
//////////////快给分!!!!!!!!!!!!!!!!!!
原型:extern char *strtok(char *s, char *delim);
用法:#include <string.h>
功能:分解字符串为一组标记串。s为要分解的字符串,delim为分隔符字符串。
说明:首次调用时,s必须指向要分解的字符串,随后调用要把s设成NULL。
strtok在s中查找包含在delim中的字符并用NULL('\0')来替换,直到找遍整个字符串。
返回指向下一个标记串。当没有标记串时则返回空字符NULL。
举例:
// strtok.c
#include <syslib.h>
#include <string.h>
#include <stdio.h>
main()
{
char *s="Golden Global View";
char *d=" ";
char *p;
clrscr();
p=strtok(s,d);
while(p)
{
printf("%s\n",s);
strtok(NULL,d);
}
getchar();
return 0;
}
相关函数:strcspn,strpbrk
Top
2 楼MAGICSLIAO(MagicsLiao)回复于 2002-07-31 17:33:15 得分 0
功能:查找由在第二个串中指定的分界符分隔开的单词
例子:
Ex1. strtok("0736-8888888","-"); 返回字符串 "0736"
Ex2. strtok("0736-8888888","6-"); 返回字符串 "073"
Ex3. strtok("0736-8888888","&"); 返回字符串 "NULL"
Top
3 楼kaku_you(正义的味方)回复于 2002-07-31 17:35:35 得分 4
该函数把你的字符串需要分割的部分置成了null
例如:
char a[]="abcde\\abcde\\abcde"
处理后变成
abcde(NULL)abcde(NULL)abcde(NULL)
然后它再一个一个NULL的跳
抄一段MSDN的例子
#include <string.h>
#include <stdio.h>
char string[] = "A string\tof ,,tokens\nand some more tokens";
char seps[] = " ,\t\n";
char *token;
void main( void )
{
printf( "%s\n\n:\n", string );
/*最初的位置取得*/
token = strtok( string, seps );
while( token != NULL )
{
/* 重复取得位置 */
printf( " %s\n", token );
/* 下一个NULL取得 */
token = strtok( NULL, seps );
}
}
Top
4 楼laonao0531((QQ:173654817))回复于 2002-07-31 18:15:39 得分 0
谁能给我写strtok()函数的具体实现???Top
5 楼zf0579(楚风萧萧)回复于 2002-07-31 18:52:17 得分 0
strtok, wcstok, _mbstok
Find the next token in a string.
char *strtok( char *strToken, const char *strDelimit );
wchar_t *wcstok( wchar_t *strToken, const wchar_t *strDelimit );
unsigned char *_mbstok( unsigned char*strToken, const unsigned char *strDelimit );
Routine
Required Header
Compatibility
strtok
<string.h>
ANSI, Win 95, Win NT
wcstok
<string.h> or <wchar.h>
ANSI, Win 95, Win NT
_mbstok
<mbstring.h>
Win 95, Win NT
For additional compatibility information, see Compatibility in the Introduction.
Libraries
LIBC.LIB
Single thread static library, retail version
LIBCMT.LIB
Multithread static library, retail version
MSVCRT.LIB
Import library for MSVCRT.DLL, retail version
Return Value
All of these functions return a pointer to the next token found in strToken. They return NULL when no more tokens are found. Each call modifies strToken by substituting a NULL character for each delimiter that is encountered.
Parameters
strToken String containing token(s)
strDelimit Set of delimiter characters
Remarks
The strtok function finds the next token in strToken. The set of characters in strDelimit specifies possible delimiters of the token to be found in strToken on the current call. wcstok and _mbstok are wide-character and multibyte-character versions of strtok. The arguments and return value of wcstok are wide-character strings; those of _mbstok are multibyte-character strings. These three functions behave identically otherwise.
Generic-Text Routine Mappings
TCHAR.H Routine
_UNICODE & _MBCS Not Defined
_MBCS Defined
_UNICODE Defined
_tcstok
strtok
_mbstok
wcstok
On the first call to strtok, the function skips leading delimiters and returns a pointer to the first token in strToken, terminating the token with a null character. More tokens can be broken out of the remainder of strToken by a series of calls to strtok. Each call to strtok modifies strToken by inserting a null character after the token returned by that call. To read the next token from strToken, call strtok with a NULL value for the strToken argument. The NULL strToken argument causes strtok to search for the next token in the modified strToken. The strDelimit argument can take any value from one call to the next so that the set of delimiters may vary.
--------------------------------------------------------------------------------
Warning Each of these functions uses a static variable for parsing the string into tokens. If multiple or simultaneous calls are made to the same function, a high potential for data corruption and inaccurate results exists. Therefore, do not attempt to call the same function simultaneously for different strings and be aware of calling one of these function from within a loop where another routine may be called that uses the same function. However, calling this function simultaneously from multiple threads does not have undesirable effects.
--------------------------------------------------------------------------------
Example
/* STRTOK.C: In this program, a loop uses strtok
* to print all the tokens (separated by commas
* or blanks) in the string named "string".
*/
#include <string.h>
#include <stdio.h>
char string[] = "A string\tof ,,tokens\nand some more tokens";
char seps[] = " ,\t\n";
char *token;
void main( void )
{
printf( "%s\n\nTokens:\n", string );
/* Establish string and get the first token: */
token = strtok( string, seps );
while( token != NULL )
{
/* While there are tokens in "string" */
printf( " %s\n", token );
/* Get next token: */
token = strtok( NULL, seps );
}
}
Output
A string of ,,tokens
and some more tokens
Tokens:
A
string
of
tokens
and
some
more
tokens
String Manipulation Routines
See Also strcspn, strspn, setlocale
Top
6 楼zf0579(楚风萧萧)回复于 2002-07-31 19:22:33 得分 5
答案 to laonao0531((交流多提高的快)):
我给出的函数实现如下,你可以检验一下,呵呵。
char* strtok(char *strToken, const char *strDelimit)
{
char *p;
char *pd;
static *ps
if (strToken != NULL)
ps = strToken;
p = ps;
pd = strDelimit;
while (*ps != '\0')
{
strDelimit = pd;
while (*strDelimit != '0') && (*strDelimit != *ps))
strDelimit ++;
ps++;
}
return p;
}
Top
7 楼zf0579(楚风萧萧)回复于 2002-07-31 19:24:56 得分 0
我不知道这样写对否,因为我还没有用VC检验之,因为看你要函数的定义,所以随便写个凑合凑合。呵呵。如果有错误欢迎批评指正。Top
8 楼LeeMaRS(小菜虎,仍需努力)回复于 2002-07-31 19:31:43 得分 4
to 楼上 : 偶试了一下,你的函数似乎不能工作.
偶写的,显得麻烦了些:
char* strtok(char *start, const char *pattern)
{
static char *last_start;
static int end;
char *r_start, *pattern_pos;
int flag=0;
if (end==1)
return NULL;
if (start!=NULL)
{
last_start=start;
end=0;
}
else
start=last_start;
r_start=start;
for (;*start!='\0';start++)
{
flag=1;
for (pattern_pos=(char *)pattern;*pattern_pos!='\0';pattern_pos++)
if (*start == *pattern_pos)
{
flag=0;
r_start++;
break;
}
if (flag==1)
break;
}
for (;*start!='\0';start++)
for (pattern_pos=(char *)pattern;*pattern_pos!='\0';pattern_pos++)
if (*start == *pattern_pos)
{
*start = '\0';
last_start=start+1;
return r_start;
}
end=1;
return r_start;
}
Top
9 楼zf0579(楚风萧萧)回复于 2002-07-31 19:35:34 得分 4
to LeeMaRS(小菜虎_水壶的仇人) 我的程序为什么不能执行啊? 有什么问题啊? 麻烦你贴一下现象。不好意思static *ps改为static char *ps, 呵呵。Top
10 楼zf0579(楚风萧萧)回复于 2002-07-31 19:39:28 得分 0
to LeeMaRS(小菜虎_水壶的仇人)
你的代码太长,不够简洁,我建议你优化优化 。另外,
if (end==1)
return NULL;
end没有赋初值,怎么可以与1比较?难道是系统给它赋的吗?我没理解,呵呵。Top
11 楼killjoy(伪装者)回复于 2002-07-31 20:43:52 得分 0
char * strtok1(char * s1,const char *s2)
{
register const char * sp;
char * Ss;
char * tok;
if (s1) Ss=(char *)s1;
while (*Ss)
{
for (sp=s2;*sp;sp++)
if (*sp==*Ss)
break;
if (*sp==0)
break;
Ss++;
}
if (*Ss==0)
return 0;
tok=Ss;
while (*Ss)
{
for (sp=s2;*sp;sp++)
if (*sp==*Ss)
{
*Ss++=0;
return tok;
}
Ss++;
}
return tok;
}
我把C语言函数库的原程序贴出来了,希望对大家有帮助!Top
12 楼rockhard(不签名)回复于 2002-08-01 09:45:15 得分 0
我给出的那个例子有点问题,不能说明问题,运行下面的例子你就会明白
#include <string.h>
#include <stdio.h>
main()
{
char s[]="Wang NIang Huangddddangccccang ldldldang XXXXXXXX";
char *d="ang";//在s串中将包含有"ang"的位置处加上NULL,注意空格的输出
char *p;
p=strtok(s,d);//第一次调用,传要处理的串参数地址s
while(p)
{
printf("%s\n",p);
p=strtok(NULL,d);//此处是第二次(或以上)调用strtok,所以第一参数为NULL,而不是s,程序知道在s中继承查找.
}
getchar();
return 0;
}
Top
13 楼zf0579(楚风萧萧)回复于 2002-08-01 14:15:55 得分 0
希望这次修订版本没有错误。呵呵,离自己理想的一次手写无错代码还是有距离啊
char* strtok(char *strToken, const char *strDelimit)
{
char *p;
char *pd;
static char *ps
if (strToken != NULL)
ps = strToken;
p = ps;
while (*ps != '\0')
{
pd = strDelimit;
while (*pd != '0') && (*pd != *ps))
pd ++;
if (*pd == *ps)
{
*ps = NULL;
ps++;
return p;
}
else
ps++;
}
ps = NULL;
}Top
14 楼polymorph(多态)回复于 2002-08-05 22:39:02 得分 0
strtok
Synopsis
#include <string.h>
char *strtok(char * restrict s1,const char * restrict s2);
Description
A sequence of calls to the strtok function breaks the string pointed to by s1 into a sequence of tokens, each of which is delimited by a character from the string pointed to by s2. The first call in the sequence has a non-null first argument; subsequent calls in the sequence have a null first argument. The separator string pointed to by s2 may be different from call to call.
The first call in the sequence searches the string pointed to by s1 for the first character that is not contained in the current separator string pointed to by s2. If no such character is found, then there are no tokens in the string pointed to by s1 and the strtok function returns a null pointer. If such a character is found, it is the start of the first token.
Top





