读书人

如何样拆分字符串最好不要涉及到复杂

发布时间: 2012-02-17 17:50:42 作者: rapoo

怎么样拆分字符串,最好不要涉及到复杂的指针,新手问题
知道怎么拆分字符串吗?比如我随即输入了一串,
rule[100]:if1 pass ip 10.1.0.88 to 255.255.255.0 p tcp port 23
然后按照空格去分开他们,然后判断是什么字符串,IP的,TCP的这样,我觉得不分开他们就不能判断阿 ,如果能不拆开就能判别里面是不是含有需要的字符串也可以。但是因为里面可能有数字,我觉得还是分出来比较容易进行数据运算



[解决办法]
#include <stdio.h>
#include <stdlib.h>

int main()
{
char rule[]= "if 1 pass ip 10.1.0.88 to 255.255.255.0 p tcp port 23 ";
char tmp[20]={0};
int index=0;
//其他...

while(index <strlen(rule))
{
sscanf(rule+index, "%s ", tmp);
index += strlen(tmp)+1;
puts(tmp);
//... 其他操作,比如判断地址 等
}
system( "PAUSE ");
return 0;
}

读书人网 >C语言

热点推荐