C语言<string.h>之strtok函数

来源:互联网 发布:模特 李荣浩 知乎 编辑:程序博客网 时间:2024/05/16 09:11
函数原型:
char *strtok(char s[], const char *delim);


功能:

分解字符串为一组字符串。s为要分解的字符串,delim为分隔符字符串。
例如:strtok("abc,def,ghi",","),最后可以分割成为abc def ghi.尤其在点分十进制的IP中提取应用较多。


返回值:

从s开头开始的一个个被分割的串。当查找不到delim中的字符时,返回NULL。
所有delim中包含的字符都会被滤掉,并将被滤掉的地方设为一处分割的节点。


简单应用:

统计一个字符串里面单词的数量
#include<string.h>#include<stdio.h>int main(void){    char input[]="You have to believe in yourself. That’s the secret of success. ";    char*p;    int count=0;   for(p=strtok(input," ");p!=0;p=strtok(NULL," ")){        printf("%s\n",p);        count++;       }    printf("%d",count);   return 0;}


1 0
原创粉丝点击