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. 剑指offer 面试10题

    面试10题: 题目:大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项.n<=39 n=0时,f(n)=0 n=1时,f(n)=1 n>1时,f(n)=f(n-1 ...

  2. 剑指offer 面试18题

    面试18题: 题目:删除链表中的节点 题一:在O(1)时间内删除链表节点.给定单向链表的头指针和一个节点指针,定义一个函数在O(1)时间内删除该节点. 解题思路:我们要删除节点i,先把i的下一个节点j ...

  3. ubuntu搭建mib2c环境

    1.下载net-snmphttp://net-snmp.sourceforge.net/download.html例如,下载5.5版本2.进入下载目录,解压net-snmp压缩包#tar zxf ne ...

  4. 【数学建模】MATLAB学习笔记——函数式文件

    MATLAB学习笔记——函数式文件 引入函数式文件 说明: 函数式文件主要用于解决计算中的参数传递和函数调用的问题. 函数式的标志是它的第一行为function语句. 函数式文件可以有返回值,也可以没 ...

  5. android各种组件的监听器

    <一>Spinner(旋转按钮或下拉列表):设置监听器为:setOnItemSelectedListener 设置动画效果为:setOnTouchListener              ...

  6. iOS oc 调用 swift

    如股票oc要调用swift里面的代码 需要包含固定这个头文件 项目名称 LiqunSwiftDemo-Swift.h #ProjectName#-Swift.h 固定的写法 swift 目的 是取代o ...

  7. kafka connect简介以及部署

    https://blog.csdn.net/u011687037/article/details/57411790 1.什么是kafka connect? 根据官方介绍,Kafka Connect是一 ...

  8. sublime text - vintage

    使用sublime text的vim模式的同学注意了: { "color_scheme": "Packages/Color Scheme - Default/Mac Cl ...

  9. json教程系列(4)-optXXX方法的使用

    在JSONObject获取value有多种方法,如果key不存在的话,这些方法无一例外的都会抛出异常.如果在线环境抛出异常,就会使出现error页面,影响用户体验,针对这种情况最好是使用optXXX方 ...

  10. centos磁盘安装与磁盘分区方案

    概述 关于centos分区的相关知识 无论怎么分区并不会影响系统文件目录的布局,如果只分/和swap这两个区 没有 usr , var , etc 等分区,在安装好后文件根目录里依然会有usr , v ...