strtok函数的使用与实现

一个用来分割字符串的函数:

strtok

char * strtok ( char * str, const char * delimiters );

Split string into tokens

A sequence of calls to this function split str into tokens, which are sequences of contiguous characters separated by any of the characters that are part of delimiters.

On a first call, the function expects a C string as argument for str, whose first character is used as the starting location to scan for tokens. In subsequent calls, the function expects a null pointer and uses the position right after the end of last
token as the new starting location for scanning.

To determine the beginning and the end of a token, the function first scans from the starting location for the first character not contained in delimiters (which becomes the beginning of the token). And then scans starting
from thisbeginning of the token for the first character contained in delimiters, which becomes the end of the token. The scan also stops if the terminating null character is found.

This end of the token is automatically replaced by a null-character, and the beginning of the token is returned by the function.

Once the terminating null character of str is found in a call to strtok, all subsequent calls to this function (with a null pointer as the first argument) return a null pointer.

The point where the last token was found is kept internally by the function to be used on the next call (particular library implementations are not required to avoid data races).

Parameters

str
C string to truncate.

Notice that this string is modified by being broken into smaller strings (tokens).

Alternativelly, a null pointer may be specified, in which case the function continues scanning where a previous successful call to the function ended.

delimiters
C string containing the delimiter characters.

These may vary from one call to another.

Return Value

A pointer to the last token found in string.

A null pointer is returned if there are no tokens left to retrieve.

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <memory.h>
#include <vector>
#include <algorithm>
#include <stack>
using namespace std;
const int SIZE = 100;

char* mystrtok(char* str, const char* delim) {
  static int len, offset;
  static char mp[256];
  static char* strcpy;
  if (str == NULL) {
    if(++offset >= len)
      return NULL;

    for(; offset < len && mp[*(strcpy + offset)]; ++offset);
    int cur = offset;
    for(; offset < len && mp[*(strcpy + offset)] == 0; ++offset);
    *(strcpy + offset) = ‘\0‘;

    return *(strcpy+cur) == ‘\0‘ ? NULL:(strcpy+cur);
  }
  else {
    memset(mp, 0, sizeof(mp));
    len = strlen(str);

    for(; *delim; ++delim)
      mp[*delim] = 1;
    strcpy = str;
    for(offset = 0; offset < len && mp[*(str + offset)]; ++offset);
    int cur = offset;
    for(; offset < len && mp[*(str + offset)] == 0; ++offset);
    *(str + offset) = ‘\0‘;

    return *(str+cur) == ‘\0‘ ? NULL:(str+cur);
  }
}
int main() {
  char str[] ="- This, a sample string.";
  char strcpy[] = "- This, a sample string.";
  char * pch;

  printf ("Splitting string \"%s\" into tokens:\n",str);
  pch = strtok (str," ,.-");
  while (pch != NULL)
  {
    printf ("%s\n",pch);
    pch = strtok (NULL, " ,.-");
  }

  printf ("Splitting string \"%s\" into tokens:\n",str);
  pch = mystrtok (strcpy," ,.-");
  while (pch != NULL)
  {
    printf ("%s\n",pch);
    pch = mystrtok (NULL, " ,.-");
  }

  return 0;
}

strtok函数的使用与实现

时间: 2024-10-28 15:31:24

strtok函数的使用与实现的相关文章

Hdu 1106 排序 (atoi函数与 strtok函数的应用

链接: http://acm.hdu.edu.cn/showproblem.php?pid=1106 好久都没刷题了,今天突然特别怀念以前刷题的日子,所以就找了几道水题来做做~~呵呵 在写这篇博客之前呢,已经很明了自己已经大三了,时光匆忙,在还没来得及转过头来,就已经过了两年了大学,原来走了这么远了~~感觉再怎么样,大三还是不敢偷懒~~不过有时候还真是挺迷茫的说实话,在这里呢,真心希望能得到你们各位的建议,只要是对我未来就业有好处的,我都会虚心取纳,(BTW:我是学嵌入式方向的)万分感谢~~~

用strtok函数分割字符串

用strtok函数分割字符串 需要在loadrunner里面获得“15”(下面红色高亮的部分),并做成关联参数. //Body response 内容: <BODY><; PRE>//OK[8,7,5,15,6,5,0,4,0,3,0,3,2,0,0,0,1 用web_reg_save_param取出“8,7,5,15,6,5,0,4,0,3,0,3,2,0,0,0,1”这一段,然后用strtok函数切割出一个个数字,第四个数字就是要找的值 例如: extern char * st

strtok函数

strtok函数是cstring文件里的函数 strtok函数是cstring文件里的函数 其功能是截断字符串 原型为:char *strtok(char s[],const char *delin); s[]是要截断的字符串,delin是用来截断的字符串. 每次调用成功后则返回切割出片段的指针. 比如,strtok("aaa,sa",",");第一次运行就会返回','之前的aaa #include<iostream> #include<cstri

strtok函数的简单应用 hdu 1106

排序 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 38231    Accepted Submission(s): 10832 Problem Description 输入一行数字,如果我们把这行数字中的'5'都看成空格,那么就得到一行用空格分割的若干非负整数(可能有些整数以'0'开头,这些头部的'0'应该被忽略掉,除非这个整数就是

关于strtok函数【转】

strtok()这个函数大家都应该碰到过,但好像总有些问题, 这里着重讲下它 首先看下MSDN上的解释: char *strtok( char *strToken, const char *strDelimit ); Parameters strToken String containing token or tokens. strDelimit Set of delimiter characters. Return Value Returns a pointer to the next tok

strtok函数读写冲突问题

先上测试代码 #include "stdafx.h" #include <iostream> using namespace std; int _tmain(int argc, _TCHAR* argv[]) { const char* split = ";"; char* str = "Hello;world;"; //result = NULL; char* result = strtok(str, split); cout &l

C++中关于strtok()函数的用法

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

strtok()函数

strtok()这个函数大家都应该碰到过,但好像总有些问题, 这里着重讲下它 首先看下MSDN上的解释: char *strtok( char *strToken, const char *strDelimit ); Parameters strToken String containing token or tokens. strDelimit Set of delimiter characters. Return Value Returns a pointer to the next tok

C语言strtok()函数:字符串分割

头文件:#include <string.h> 定义函数:char * strtok(char *s, const char *delim); 函数说明:strtok()用来将字符串分割成一个个片段.参数s 指向欲分割的字符串,参数delim 则为分割字符串,当strtok()在参数s 的字符串中发现到参数delim 的分割字符时则会将该字符改为\0 字符.在第一次调用时,strtok()必需给予参数s 字符串,往后的调用则将参数s 设置成NULL.每次调用成功则返回下一个分割后的字符串指针.