strtok函数详解

本文深入解析了strtok函数的工作原理及实现细节,并通过实例演示了如何利用该函数进行字符串分割。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

strtok函数,将字符串分解成一组字符串,使用时,包含头文件 #include <string.h>即可,下面是该函数的原型:

char *strtok(char s[], const char *delim);
其中,s为要分解的字符串,delim为分隔符字符串。首次调用时,s指向要分解的字符串,之后再次调用要把s设为NULL

本文详细分析下该函数分解字符串的过程。下面是strtok的源码,在Visual Studio 下使用单步调试即可看到:

/***
*strtok.c - tokenize a string with given delimiters
*
*         Copyright (c) Microsoft Corporation. All rights reserved.
*
*Purpose:
*         defines strtok() - breaks string into series of token
*         via repeated calls.
*
*******************************************************************************/
#include <cruntime.h>
#include <string.h>
#ifdef _MT
#include <mtdll.h>
#endif  /* _MT */
/***
*char *strtok(string, control) - tokenize string with delimiter in control
*
*Purpose:
*         strtok considers the string to consist of a sequence of zero or more
*         text tokens separated by spans of one or more control chars. the first
*         call, with string specified, returns a pointer to the first char of the
*         first token, and will write a null char into string immediately
*         following the returned token. subsequent calls with zero for the first
*         argument (string) will work thru the string until no tokens remain. the
*         control string may be different from call to call. when no tokens remain
*         in string a NULL pointer is returned. remember the control chars with a
*         bit map, one bit per ascii char. the null char is always a control char.
* 
*Entry:
*         char *string - string to tokenize, or NULL to get next token
*         char *control - string of characters to use as delimiters
*
*Exit:
*         returns pointer to first token in string, or if string
*         was NULL, to next token
*         returns NULL when no more tokens remain.
*
*Uses:
*
*Exceptions:
*
*******************************************************************************/
char * __cdecl strtok (
          char * string,
          const char * control
          )
{
        unsigned char *str;
        const unsigned char *ctrl = control;
        unsigned char map[32];
        int count;
#ifdef _MT
        _ptiddata ptd = _getptd();
#else  /* _MT */
        static char *nextoken;  //静态变量,保存剩余子串的      
#endif  /* _MT */
        /* 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
#ifdef _MT
                str = ptd->_token;
#else  /* _MT */
                str = nextoken; 
#endif  /* _MT */
        /* 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 */
#ifdef _MT
          ptd->_token = str;
#else  /* _MT */
        nextoken = str;
#endif  /* _MT */
        /* Determine if a token has been found. */
        if ( string == str )
                return NULL;
        else
                return string;
}

这个程序不难,上面的map数组相当于一个映射表,将分割字符串中的字符映射到该数组中。map数组大小为32是因为一个字符占8bit,ASCII码值范围在0~255,右移3位(即除以8)之后必在32之内。

下面结合一个例子来讲述下这个函数的运行过程:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
	char s[] = "abadcbaf";
	printf("s = %p, %s\n", s ,s);
	char *p = strtok(s, "ab");
	printf("p = %p, %s\n", p, p);
	printf("s = %p, %s\n", s ,s);
	while (p) {
		p = strtok(NULL, "ab");
		printf("p = %p, %s\n", p, p);
		printf("s = %p, %s\n", s ,s);
	}
	system("pause");
	return 0;
}

该样例的输出结果为:

s = 00FAFDBC, abadcbaf
p = 00FAFDBF, dc
s = 00FAFDBC, abadc
p = 00FAFDC3, f
s = 00FAFDBC, abadc
p = 00000000, (null)
s = 00FAFDBC, abadc

s是一个字符串数组,在内存中占用9Bytes空间,如下图所示。

首次调用strtok时,程序执行到79行之前,指针指向关系如下,string和str都指向数组s的第一个元素。


执行完82行之后,如下图所示。79和80两行主要跳过分隔符字符串中的字符。


接着执行到85行这个for循环,如果遇到分割字符串中的字符,则将该字符设为'\0',并指向下一个字符串,因此该函数会修改原字符串。

95行,将str的值赋给静态变量nextoken保存剩余的字符串。


最后返回string。注意数组s的首地址是不会变的,因为调用strtok传参是值传递。

第二次调用strtok时,strtok的第一个参数为NULL,在74行,str指向nextoken指向的位置。


然后同上,会跳过分割字符串中的字符。


最后,str和nextoken都指向字符串末尾,返回string。


下一次再调用strtok时,98行条件成立,返回NULL。

 

注意:若原串s中没有分割字符串中任何字符,则在第一次调用时返回原串,第二次调用时才返回NULL。

对于上面的例子,若调用p = strtok(s, "#");则返回的就是数组首地址值。第二次调用strtok(NULL,"#")才返回NULL。


另外,与strtok相似的一个函数strtok_s,是strtok的安全版本,原型如下:

char *strtok_s( char *strToken, const char*strDelimit, char **buf);

该函数的前两个参数与strtok一样,第三个参数功能和strtok中的静态变量nextoken功能一样,保存剩余字符串的位置。








                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值