检测ip字符串是否合法
2014-12-29 17:17
183 查看
#include <stdio.h> #include <string.h> #include <assert.h> typedef enum {false, true} bool; bool checkIsIP_Valid(char *s) { assert( s != NULL ); char sec[3]; int num = 3; int dot_count = 0; memset(sec, '\0', 3); while( *s != '\0' ) { if( *s >= '0' && *s <= '9' ) { //1234.x.x.x if( num < 0 ) { return false; } else { sec[ 3-num ] = *s; num--; } } else if( *s == '.' ) { // .x.x.x if( num == 3 ) { return false; } //369.x.x.x if( atoi(sec) > 255 ) { return false; } //fprintf(stderr, "%d.", atoi(sec)); //reset num and sec[] num = 3; memset(sec, '\0', 3); dot_count++; } else { return false; } s++; } if( num == 3 || atoi(sec) > 255 || dot_count != 3 ) { return false; } else { fprintf(stderr, "\n"); return true; } } //test char *casen[] = { "1.2.3.369", "1.2.369.4", ".2.3.4", "1.2..4", "1.2.3", "a.2.3.4", "1.2.3.4" }; int main() { int i; for( i=0; i<7; i++ ) { fprintf(stderr, "%s is %s\n", casen[i], checkIsIP_Valid( casen[i] ) == true ? "valid" : "invalid"); } }
相关文章推荐
- 检测一个字符串ip是否合法
- Python检测IP合法 是否为公网IP
- 判断一个字符串是否为合法IP
- 全正则的检测IP是否合法的函数
- 正则表达式检测ip是否合法
- 字符串——判断IP是否合法
- shell 函数实现检测字符串是否为合法的 IPv4 地址
- shell检测输入的IP是否合法
- android 检测字符串是否为合法域名
- 判断某个字符串是否是合法IP
- 检测日期字符串是否为合法(js版)
- 判断一个字符串的ip是否是IP合法的ip地址
- 判断字符串是否为合法ip
- 全正则的检测IP是否合法的函数
- iOS 检测字符串输入是否为合法数字
- 检测xml字符串是否合法
- 检测日期字符串是否为合法(java版)
- 检测IP输入是否合法
- 判断一个字符串的ip是否是IP合法的ip地址
- 判断字符串是否是合法的EMail