合法IP检测
来源:互联网 发布:nginx.var set lua 编辑:程序博客网 时间:2024/05/16 13:04
**合法IP检测:
首先检测是否有非法字符,
然后检测点的数量,
然后检测是否有点在开头或结尾或有连续的点连在一起以及每个部分的位数及数值是否合法。**
#include <stdio.h>#include <string.h>const int maxn = 110;char buf[maxn], buf2[maxn];bool isValidChar(const char *str) { for (int i = 0; str[i]; ++i) if (str[i] != '.' && !(str[i] >= '0' && str[i] <= '9')) return false; return true;}int countDot(char * str) { int cnt = 0; for (int i = 0; str[i]; ++i) if (str[i] == '.') ++cnt; return cnt;}bool check(const char *str) { int digit = 0, id = 0, len; for (int i = 0; str[i]; ++i) { if (str[i] == '.') { buf2[id] = '\0'; if (*buf2 == '\0') return false; // two '.' connected or begin with '.' else if ((len = strlen(buf2)) > 1 && *buf2 == '0' || len > 3) return false; // .012. else if (digit > 255) return false; digit = id = 0; } else { digit = digit * 10 + str[i] - '0'; buf2[id++] = str[i]; } } return true;}int main() { while (gets(buf)) { int len = strlen(buf); buf[len++] = '.'; // Easy to handle buf[len] = '\0'; if (!isValidChar(buf) || countDot(buf) != 4 || !check(buf)) puts("NO"); else puts("YES"); } return 0;}
阅读全文
0 0
- 合法IP检测
- 合法IP检测
- Shel 检测合法的IP地址
- 检测IP地址是否真正合法的函数
- 检测输入框中是否是合法的IP地址
- 合法IP
- 合法IP
- 合法IP
- 合法IP
- 合法IP
- 合法IP
- 合法IP
- 判断IP合法
- 华为OJ ----合法IP
- 华为oj-合法IP
- 合法ip序列
- 【华为oj】合法ip
- 华为oj 合法ip
- 解决IE8不支持数组的indexOf方法
- SVN基础
- 纯干货!视频控件VideoCapX的使用指南和常见问题合集
- hdu1533 Going Home KM算法求最小费用
- 高效算法设计_贪心法(最优装载问题,部分背包问题,乘船问题)
- 合法IP检测
- TextWatcher的用法
- Andrew NG 深度学习课程笔记:神经网络、有监督学习与深度学习
- linux普通用户进程数受限制报错
- 5-7 六度空间 (bfs)
- 懒加载
- Removing Columns
- Java-----隐藏手机号中间四位
- NYOJ【22】素数求和问题【水题】