首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 其他相关 >

检测一个字符串ip是不是合法

2012-10-12 
检测一个字符串ip是否合法int check_ip(char *ip){char * pipint count0while(*p ! \0){if( *p

检测一个字符串ip是否合法

int check_ip(char *ip){char * p=ip;int count=0;while(*p != '\0'){if( *p == '.') count++;p++;}if( count != 3 )return false;p=ip;for(int i=0;i<4;++i){int len=0;int tem=0;while(*p!=NULL){if(*p=='.')break;tem=tem*10+*p-'0';p++;len++;}if(len==1){if(tem<0||tem>9)return false;}else if(len==2){if(tem<10 ||tem>99)return false;}else if(len==3){if(tem<100||tem>255)return false;}else return false;if(*p!='\0')p++;}return true;}

先检测是不是有三个点

然后一次检查每个部分的取值是否在合法的范围内

热点排行