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

    切割字符串是常用的处理。

    这里给出一个使用函数strtok切割字符串的例子。

    使用C语言的库函数strtok来切割字符串的好处在于,可以指定任意字符作为分隔符来切割单词。使用该函数,切割字符串的分隔符可以同时指定多个,放在一个字符串数组中。

    程序中,指定了以空格“ ”、逗号“,”和句号“.”作为分隔符。

    程序如下:

    /* B00009 C语言分割字符串库函数strtok */
    
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
    
        char s[]="So, you've never programmed before. As we go through this tutorial,I will attempt to teach you how to program.";
        char delim[] = " ,.";
        char *p;
    
        p = strtok(s, delim);
        while(p) {
             printf("%s
    ",p);
             p = strtok(NULL, delim);
        }
    
        return 0;
    }

    运行结果如下:

    So
    you've
    never
    programmed
    before
    As
    we
    go
    through
    this
    tutorial
    I
    will
    attempt
    to
    teach
    you
    how
    to
    program



  • 相关阅读:
    【XSY2505】tree
    【XSY2558】圆上的蚂蚁 Ants on circle
    【模板】生成函数
    左偏树
    Link cut tree
    高斯消元
    cdq分治——bzoj2683简单题
    半平面交
    关于向量,凸包及旋转卡壳
    状压dp:luogu P2704 [NOI2001]炮兵阵地
  • 原文地址:https://www.cnblogs.com/tigerisland/p/7564757.html
Copyright © 2011-2022 走看看