strstok详解

extern char *strtok(char *s, char *delim);

用法:#include <string.h> 功能:分解字符串为一组标记串。s为要分解的字符串,delim为分隔符字符串。 说明:首次调用时,s必须指向要分解的字符串,随后调用要把s设成NULL。 strtok在s中查找包含在delim中的字符并用NULL('\0')来替换,直到找遍整个字符串。 返回指向下一个标记串。当没有标记串时则返回空字符NULL。

 1 #include<stdio.h>
2 #include<string.h>
3 #include<stdlib.h>
4
5 char x[20001];
6 int main()
7 {
8 int i,j,k,l;
9 char *p,*q;
10 while(gets(x))
11 {
12 if(strcmp(x,"#")==0) break;
13 l=0;
14 p=x;
15 q=strtok(p," ");
16 while(q)
17 {
18 printf("%s\n",q);
19 q=strtok(NULL," ");
20 }
21
22 }
23 return 0;
24 }

运用strtok来判断ip或者mac的时候务必要先用其他的方法判断'.'或':'的个数,因为用strtok截断的话,比如:"192..168.0...8..."这个字符串,strtok只会截取四次,中间的...无论多少都会被当作一个key

原文地址:https://www.cnblogs.com/qiufeihai/p/2323384.html