zoukankan      html  css  js  c++  java
  • C语言字符串拆分函数strtok


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

    Sequentially truncate string if delimiter is found.
      If string is not NULL, the function scans string for the first occurrence of any character included in delimiters. If it is found, the function overwrites the delimiter in string by a null-character and returns a pointer to the token, i.e. the part of the scanned string previous to the delimiter.
      After a first call to strtok, the function may be called with NULL as string parameter, and it will follow by where the last call to strtok found a delimiter.
      delimiters may vary from a call to another.

    Parameters.

    string
    Null-terminated string to scan.
    separator
    Null-terminated string containing the separators.

    Return Value.
      A pointer to the last token found in string.   NULL is returned when there are no more tokens to be found.

    Portability.
      Defined in ANSI-C.

    Example.

    /* strtok example */
    #include <stdio.h>
    #include <string.h>
    
    int main ()
    {
      char str[] ="This is a sample string,just testing.";
      char * pch;
      printf ("Splitting string \"%s\" in tokens:\n",str);
      pch = strtok (str," ");
      while (pch != NULL)
      {
        printf ("%s\n",pch);
        pch = strtok (NULL, " ,.");
      }
      return 0;
    }
    

    Output:
    Splitting string "This is a sample string,just testing." in tokens:
    This
    is
    a
    sample
    string
    just
    testing 

  • 相关阅读:
    04:求整数的和与均值
    03:均值
    02:财务管理
    C8-3 三角形还是长方形? (100/100 分数)
    C8-2 圆的周长和面积 (100/100 分数)
    C8-1 复数加减乘除 (100/100 分数)
    C7-3 用类实现a+b (100/100 分数)
    C7-2 多继承 (100/100 分数)
    C7-1 账户类(100/100)
    数组第k小数
  • 原文地址:https://www.cnblogs.com/feisky/p/1743274.html
Copyright © 2011-2022 走看看