strtok:
#include <string.h>
char *strtok(char *str, const char *delim);
char *strtok_r(char *str, const char *delim, char **saveptr);
功能:分解字符串为一组标记串。str为要分解的字符串,delim为分隔符字符串。
说明:首次调用时,str必须指向要分解的字符串,随后调用要把s设成NULL。
strtok在str中查找包含在delim中的字符并用NULL('/0')来替换,直到找遍整个字符串。
返回指向下一个标记串。当没有标记串时则返回空字符NULL。
实例:用strtok来判断ip地址是否合法:ip_strtok.c:
[c-sharp] view plaincopy
//ip_strtok.c
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv)
{
char temp_buf[] = {};
char p_temp[];
char *p=NULL;
char *t = ".";
int m,n,i;
int j=,s=; if(argc!=)
{
printf("param must 2/n");
return -;
}
strcpy(temp_buf, argv[]);
for(i=; i<strlen(temp_buf);i++)
{
if(temp_buf[i] == *t)j++;
if(temp_buf[i] == *t && (temp_buf[i+]>=''&&temp_buf[i+]<=''))
{
s++;
}
}
if(j!= || j!=s)
{
printf("ip param format error/n");
return -;
}
p = strtok(temp_buf, t); while(p!=NULL)
{
strcpy(p_temp, p);
printf("%s /n", p_temp);
for(n=; n<strlen(p_temp);n++)
{
if(!(p_temp[n]>=''&&p_temp[n]<=''))
{
printf("ip param error/n");
return -;
}
} m = atoi(p_temp);
if(m>)
{
printf("ip invalid /n");
return -;
} p=strtok(NULL, ".");
printf("p = %s/n",p);
}
printf("ok! ip correct! ip=%s/n", argv[]);
return ;
}
编译运行:
[root@localhost liuxltest]# uname -r
2.6.
[root@localhost liuxltest]# gcc -Wall ip_strtok.c -o ip_strtok
[root@localhost liuxltest]# ./ip_strtok 172.18.4.255 p = p = p = p = (null)
ok! ip correct! ip=172.18.4.255
[root@localhost liuxltest]# ./ip_strtok 172.18.
ip param format error
strtok实现函数:
xl_strtok.c
[c-sharp] view plaincopy
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
char *xl_strtok(char *str, const char *delim);
int main(int argc, char **argv)
{
if(argc != )
{
printf("param must be 3 /n");
return -;
} char *temp;
char *p_temp;
char *p;
temp=(char *)malloc();
p_temp=(char *)malloc();
strcpy(temp, argv[]);
strcpy(p_temp, argv[]);
printf("temp = %s /n",temp);
printf("p_temp = %s /n", p_temp);
p = xl_strtok(temp, p_temp);
printf("%s/n", p);
return ;
}
char *xl_strtok(char *s, const char *dm)
{
static char *last;
char *tok;
if(s == NULL)
s = last;
if(s == NULL)
return NULL;
tok = s;
while (*s != '/0')
{
while (*dm)
{
if (*s == *dm)
{
*s = '/0';
last = s + ;
break;
}
s++;
}
}
return tok;
}
strtok源码:
/*********************************************************************/
/***
*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 _SECURE_VERSION
#include <internal.h>
#else /* _SECURE_VERSION */
#include <mtdll.h>
#endif /* _SECURE_VERSION */
/***
*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:
*
*******************************************************************************/
#ifdef _SECURE_VERSION
#define _TOKEN *context
#else /* _SECURE_VERSION */
#define _TOKEN ptd->_token
#endif /* _SECURE_VERSION */
#ifdef _SECURE_VERSION
char * __cdecl strtok_s (
char * string,
const char * control,
char ** context
)
#else /* _SECURE_VERSION */
char * __cdecl strtok (
char * string,
const char * control
)
#endif /* _SECURE_VERSION */
{
unsigned char *str;
const unsigned char *ctrl = control;
unsigned char map[];
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 = ; count < ; count++)
map[count] = ;
/* Set bits in delimiter table */
do {
map[*ctrl >> ] |= ( << (*ctrl & ));
} 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 >> ] & ( << (*str & ))) && *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 >> ] & ( << (*str & )) ) {
*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;
}

C++中关于strtok()函数的用法的更多相关文章

  1. C语言中关于scanf函数的用法

    scanf()函数的控制串 函数名: scanf 功 能: 执行格式化输入 用 法: int scanf(char *format[,argument,...]); scanf()函数是通用终端格式化 ...

  2. JavaScript中字符串分割函数split用法实例

    这篇文章主要介绍了JavaScript中字符串分割函数split用法,实例分析了javascript中split函数操作字符串的技巧,非常具有实用价值,需要的朋友可以参考下 本文实例讲述了JavaSc ...

  3. C中的时间函数的用法

    C中的时间函数的用法    这个类展示了C语言中的时间函数的常用的用法. 源代码: #include <ctime>#include <iostream> using name ...

  4. (转)Python中的split()函数的用法

    Python中的split()函数的用法 原文:https://www.cnblogs.com/hjhsysu/p/5700347.html Python中有split()和os.path.split ...

  5. 数据库SQL中case when函数的用法

    Case具有两种格式,简单Case函数和Case搜索函数.这两种方式,可以实现相同的功能.简单Case函数的写法相对比较简洁,但是和Case搜索函数相比,功能方面会有些限制,比如写判断式. 简单Cas ...

  6. mysql中的group_concat函数的用法

    本文通过实例介绍了MySQL中的group_concat函数的使用方法,比如select group_concat(name) . MySQL中group_concat函数 完整的语法如下: grou ...

  7. Java中的split函数的用法

    Java中的 split  函数是用于按指定字符(串)或正则去分割某个字符串,结果以字符串数组形式返回: 例如: String str="1234@abc"; String[] a ...

  8. PHP中日期时间函数date()用法总结

    date()是我们常用的一个日期时间函数,下面我来总结一下关于date()函数的各种形式的用法,有需要学习的朋友可参考. 格式化日期date() 函数的第一个参数规定了如何格式化日期/时间.它使用字母 ...

  9. Python中的join()函数的用法

    函数:string.join() Python中有join()和os.path.join()两个函数,具体作用如下:    join():    连接字符串数组.将字符串.元组.列表中的元素以指定的字 ...

随机推荐

  1. 玩转type类型(牛逼克拉斯 )

    一.前言 一说起type()方法肯定有很多人都知道这不就是查看某个对象类型的嘛,其实不然,他还有更牛逼的应用------创建类 >>> type(1) <class 'int' ...

  2. source $0 & bash $0

    bash file.sh这种形式启动时,$0才被设置成文件名. source是bash的内建命令,把文件读取到当前bash中执行,相当于在当前bash手动输入命令,所以$0是bash. 示例代码如下: ...

  3. Hadoop学习基础之三:MapReduce

    现在是讨论这个问题的不错的时机,因为最近媒体上到处充斥着新的革命所谓“云计算”的信息.这种模式需要利用大量的(低端)处理器并行工作来解决计算问题.实际上,这建议利用大量的低端处理器来构建数据中心,而不 ...

  4. loadrunner之脚本篇——录制方式HTML-based和URL-based Script

    A.   HTML-based Script 针对 Web (HTTP/HTML)虚拟用户的缺省录制级别.它指示VuGen录制当前web页面上下文中的HTML action.录制会话期间并不录制所有资 ...

  5. springboot——数据层访问搭建 集成Duid连接池

    springboot中默认是使用的tomcat的连接池,如果我们想要第三方的连接池,我们这么配置呢? 首先在application.yml文件中注释掉之前数据库的配置,重新用druid的方式配置: # ...

  6. cocos打包出现错误,执行命令出错,返回值:2。 Traceback (most recent call last): File "E:\cocos_workspace\MyGameOne\proj.android\build_native.py", line 43, in <module> build(opts.build_mode) File "E:\cocos_workspace\MyGa

    先看看NDK的版本,如果不行,就删除\proj.android\obj\local\armeabi下的文件.

  7. Linux下SPI测试程序

    /** 说明:SPI通讯实现* 方式一: 同时发送与接收实现函数: SPI_Transfer()* 方式二:发送与接收分开来实现* SPI_Write() 只发送* SPI_Read() 只接收* 两 ...

  8. [NOI2008]设计路线

    题目 洛谷 BZOJ 做法 神仙题 显然这是棵树 个节点相东仅连接一个结点 不同于剖分,还能存在\("V"\)字型,一个节点最多与另外节点连两条边 \(dp[i][j][k]\)表 ...

  9. JSP语法及内置对象

    JSP全名为Java Server Pages,中文名叫java服务器页面,其根本是一个简化的Servlet设计,它[1]  是由Sun Microsystems公司倡导.许多公司参与一起建立的一种动 ...

  10. Linux bridge

    CentOS bridge 配置: 1.创建br0配置文件  touch /etc/sysconfig/network-scripts/ifcfg-br0 2.修改bro配置文件 vi /etc/sy ...