数据结构——算法之(039)(ip合法性检查)
来源:互联网 发布:电信计费模拟c语言 编辑:程序博客网 时间:2024/05/21 06:44
【申明:本文仅限于自我归纳总结和相互交流,有纰漏还望各位指出。 联系邮箱:Mr_chenping@163.com】
题目:
ip合法性检查
题目分析:
(1) 字符串只含有数字和'.'
(2) 长度在7-15之间(0.0.0.0--255.255.255.255)
(3) 每个数字在0-255之间
(4) '.'号不能超过3个,且不能连续
算法实现:
#include <stdio.h>#include <string.h>#define MAX_LEN 15 //255.255.255.255#define MIN_LEN 7 //0.0.0.0int str_to_int(const char *begin, const char *end){ int total = 0; const char *s = begin; while(s < end) { total = 10*total + (*s - '0'); s++; } printf("total=%d\n", total); return total;}/*** ip合法性检查** (1) 字符串只含有数字和'.'** (2) 长度在7-15之间(0.0.0.0--255.255.255.255)** (3) 每个数字在0-255之间** (4) '.'号不能超过3个,且不能连续*/int check_ip(const char *str){ int len = strlen(str); if(!str || len > MAX_LEN || len < MIN_LEN) return 0; const char *slow = str, *fast = str; int count_dot = 0, tmp; while(*fast != '\0') { if(*fast != '.' && (*fast < '0' || *fast > '9')) return 0; while(*fast != '.') { if(*fast == '\0') break; fast++; } if(*fast == '.') { if(++count_dot > 3) //.数量大于3个 return 0; } if(*fast == '.' && *slow == '.') //两个.挨着 return 0; tmp = str_to_int(slow, fast); if(tmp < 0 || tmp > 255) return 0; slow = ++fast; } return 1;}int main(){ char str[] = "01.16"; //char str[] = " 01.168.1.2"; //char str[] = "01.168..2"; //char str[] = "192.168.2.1000"; printf("-------->%d\n", check_ip(str));}
0 0
- 数据结构——算法之(039)(ip合法性检查)
- c语言利用inet_pton 函数检查ip合法性(转)
- IP地址合法性检查
- shell检查IP合法性
- shell: ip合法性检查
- VBA 检查IP的合法性
- Shell对IP合法性检查
- Delphi检查IP地址的合法性
- 正则表达式检查IP地址合法性
- 利用inet_pton 函数检查ip合法性
- 【数据结构】检查元素出栈的合法性
- freeDiameter源码阅读之 dictionary树和消息的合法性检查(rule)
- IP地址合法性(模拟逻辑运算)
- 数据结构与算法学习之路:检查表达式的括号是否匹配——栈
- 【身份证合法性检查程序】(计算最后一位检验码)
- 合法性检查
- 使用awk/grep检查IP地址的合法性
- Shell脚本判断IP是否合法性(多种方法)
- Activity及其生命周期小结
- 美国高考SAT数学新政介绍
- Spark-1.3.1与Hive整合实现查询分析
- FrameLayout API
- LRUCache 详解
- 数据结构——算法之(039)(ip合法性检查)
- shell排序
- GCD学习笔记(二):dispatch_queue_create
- 第14周 项目三-OOP版电子词典
- 第十四周程序阅读——例十三
- GRE作文怎样用问句开头?
- 第十三周 课后实践:项目三——立体类族共有的抽象类
- GCD学习笔记(三):Main Dispatch Queue/Global Dispatch Queue
- cocos2d-x 3.6连连看工程结构