How does strtok() split the string into tokens in C?

前端 未结 15 2085
陌清茗
陌清茗 2020-11-22 14:48

Please explain to me the working of strtok() function. The manual says it breaks the string into tokens. I am unable to understand from the manual what it actua

15条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-22 15:30

    For those who are still having hard time understanding this strtok() function, take a look at this pythontutor example, it is a great tool to visualize your C (or C++, Python ...) code.

    In case the link got broken, paste in:

    #include 
    #include 
    
    int main()
    {
        char s[] = "Hello, my name is? Matthew! Hey.";
        char* p;
        for (char *p = strtok(s," ,?!."); p != NULL; p = strtok(NULL, " ,?!.")) {
          puts(p);
        }
        return 0;
    }
    

    Credits go to Anders K.

提交回复
热议问题