strtok函数的使用与实现

本文介绍了一个用于字符串分割的函数strtok的使用方法,并通过示例演示了如何使用该函数来分割字符串。此外,还提供了一个将IP地址转换为整数的例子。

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

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

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;
}

IP地址转换成int:

#include <stdio.h>
#include <algorithm>
#include <string>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  
  char ip[] = "162.105.146.221", *pch = NULL;
  unsigned int ipres = 0, cur = 0, cnt = 0;

  pch = strtok(ip,".");
  while (pch != NULL) {
    sscanf(pch,"%u",&cur);
    ipres = ipres | (cur << cnt);
    cnt += 8;
    pch = strtok(NULL,".");
  }

  return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值