strtok函数的实现
原型: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;
}
源码:
/***
*strtok.c - tokenize a string with given delimiters
*
* Copyright (c) 1989-1997, 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[*ctrl >> 3] |= (1 << (*ctrl & 7))说明:
map[*ctrl >> 3] |= (1 << (*ctrl & 7));
这句是这样的意思
首先map[32]是个uchar型数组,数组每一个是8位,其中每一位可以表示一个字符,32×8=256,这样的话,map[32]中有256个bit,每个bit表示一个ASCII码,那么可以表示256个ASCII码。
*ctrl >> 3,表示将安ascii码,给其分类,*ctrl >> 3表示,除以8的意思,将ascii每八位分为一组,也就是map[32]中的一个。
1 << (*ctrl & 7),这个是这样的意思,7表示为二进制就是00000111,这样的话,相当于是一个数除以8后剩余的余数。1 << (*ctrl & 7),就是将二进制00000001,向右移动(*ctrl & 7)个位。
map[*ctrl >> 3] |= (1 << (*ctrl & 7)),就是表示将map[*ctrl >> 3]中的(*ctrl & 7)+1位设为1,表示在该位置查询到一个ascii字符。
这样做以后,就相当于简历了一个表,只要查询相应的位是否为1,就知道该字符,在strtok的字符串中是否出现过。
strtok函数的实现相关推荐
- 恶心的C语言strtok函数
从C#.JAVA到C ,让我觉得像是从公产主义社会回到了原始社会,不顺手,所以很心里憋气!!! 函数名: strtok 功 能: 查找由在第二个串中指定的分界符分隔开的单词 用 法: char ...
- C和指针之字符串strtok函数
1.strtok函数 头文件:#include <string.h> 定义函数:char * strtok(char *s, const char *delim); 函数说明:strtok ...
- 【摘录】C语言中利用 strtok函数进行字符串分割
C语言不像Java,Php之类的高级语言,对象中直接封装了字符串的处理函数.C语言中进行普通的字符串处理也经常会让我们焦头烂额--不过好在C语言 中还是提供了像strtok这样功能强大的字符串处理函数 ...
- bob-tong 字符串函数之Strtok()函数
https://www.cnblogs.com/Bob-tong/p/6610806.html Strtok()函数详解: 该函数包含在"string.h"头文件中 函数原型 ...
- C/C++根据特定字符分割字符串、读取文件去掉逗号等特定字符、strtok()函数详解
字符串分割情况 读取文件时,C++识别的是空格和换行符,但有时候文件是以符号分割的,如逗号等 字符串本身含有特殊符号,如逗号,@等 strtok()函数 strtok()函数能够按照特定的字符分解字符 ...
- C语言strtok()函数:字符串分割
1.头文件:#include <string.h> 2.定义函数:char * strtok(char *s, const char *delim); 分解字符串为一组字符串.s ...
- strstr函数和strtok函数的使用
目录: strstr函数的使用 模拟实现strstr函数 strtok函数的使用 strstr函数定义:strstr(str1,str2) 函数用于判断字符串str2是否是str1的子串.如果是,则该 ...
- strtok函数及其实现
头文件:#include <string.h> 定义函数:char * strtok(char *s, const char *delim); 函数说明:strtok()用来将字符串分割成 ...
- c strtok函数用法
strtok函数用于分割字符串,原型如下: char *strtok(char *str, const char *delim); str为指向欲分割的字符串,delim为分隔符,实例如下: #inc ...
- strtok函数的使用
函数形式: #include <string.h> char *strtok(char *str, const char *delim); 作用: 根据某个定界附,将字符串分解成多个部分. ...
最新文章
- 拼多多员工爆料:拼多多开启硬核模式!午休减半!每月工作300小时!千万别来拼多多!...
- 利用Gephi软件绘制网络图
- 数字签名、私钥、公钥
- [物理学与PDEs]第2章习题参考解答
- WINCE下实现USB转RS232
- C语言结构和高二的知识,c语言基础知识复习.pdf
- PDH-SDH光端机指示灯具体含义介绍
- 牌类游戏使用微服务重构笔记(八): 游戏网关服务器
- 微信公众平台java开发详解(工程代码+解析)
- MySQL慢查询之慢SQL定位、日志分析与优化方案
- mysql 取模分区_MySQL分区
- plsql 往视图传参数_我们可以将参数传递给SQL中的视图吗?
- 字符串交错组成--很优美的递归算法
- 如何在屏幕实时显示键盘操作(独家分享)
- 普通table表格样式及代码大全
- 【uni-app的ui组件】uni-ui如何安装使用教程
- SQL数据库置疑的解决办法
- 计算机复制功能快捷键,电脑复制快捷键是什么(全部复制粘贴的快捷键是什么)...
- Excel复制到word,清除格式后行间距过大
- 设置华为路由器的端口映射
热门文章
- 单证与双证高级证书与普通证书的区别与联系
- HTML层叠样式表(CSS)
- 【转贴】对《高质量程序设计指南--C++/C第二版》的探讨
- opencv HSV 颜色模型(H通道取值 CV_BGR2HSV_FULL)
- WSUS客户端更新补丁失败(1)
- ESRGAN - Enhanced Super-Resolution Generative Adversarial Networks论文翻译——中英文对照
- 弄明白CMS和G1,就靠这一篇了
- processing制作动态山水背景
- exlc表格怎么换行_excel如何换行_表格怎么换行上下换行
- MySQL——连接查询