判断字符串是否为IP地址,用到sscanf和sprintf
来源:互联网 发布:塔罗牌准不准知乎 编辑:程序博客网 时间:2024/05/16 11:22
思路:输入字符串的时候,把分隔符“.”读取出来,然后判断分隔符旁边的数字是否在0~~255之间,然后判断是否合法。
代码:
// IsIpAddre.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <stdio.h>
#include <string.h>
int main(void)
{
char str[31],temp[31];
int a,b,c,d;
while(gets(str)!=NULL)
{
if(sscanf(str, "%d.%d.%d.%d ",&a,&b,&c,&d)==4 && a>=0 && a<=255 && b>=0 && b<=255 && c>=0 && c<=255 && d>=0 && d<=255)
{
sprintf(temp, "%d.%d.%d.%d",a,b,c,d); //把格式化的数据写入字符串temp
if(strcmp(temp,str)==0)
{
printf("YES\n");
}
else
{
printf("NO\n");
}
}
else
{
printf("NO\n");
}
}
return 0;
}
- #include <stdio.h>
- #include <string.h>
- int main(void)
- {
- char str[31],temp[31];
- int a,b,c,d;
- while(gets(str)!=NULL)
- {
- if(sscanf(str, "%d.%d.%d.%d ",&a,&b,&c,&d)==4 && a>=0 && a<=255 && b>=0 && b<=255 && c>=0 && c<=255 && d>=0 && d<=255)
- {
- sprintf(temp, "%d.%d.%d.%d",a,b,c,d); //把格式化的数据写入字符串temp
- if(strcmp(temp,str)==0)
- {
- printf("YES\n");
- }
- else
- {
- printf("NO\n");
- }
- }
- else
- {
- printf("NO\n");
- }
- }
- return 0;
- }
- 判断字符串是否为IP地址,用到sscanf和sprintf
- 判断字符串是否为IP地址。
- c# 判断字符串是否为IP地址
- 判断字符串是否为IP地址
- 判断字符串是否为IP地址
- 判断IP 字符串是否为有效地址
- 判断是否为IP地址
- 判断是否为IP地址
- 如何判断一个字符串是否为正确的IP地址
- 判断输入的字符串是否为ip地址
- 判断字符串是否为合格的IP地址
- shell 判断字符串是否为有效ip地址
- 判断一个字符串是否为合法ip地址(ipv4)
- 判断IP地址是否为公有地址
- 判断一个字符串是否是IP地址
- sscanf(字符串转化为整数)和sprintf(整数转化为字符串)
- 判断IP地址是否为内网IP地址
- 判断IP地址是否为内网IP地址
- 管理实践日志---序
- java web培训_OA系统_北软教育_工作日志_6.26
- Oracle append Hint(三)
- sscanf和sprintf的作用简介(一)
- html 锚点的使用
- 判断字符串是否为IP地址,用到sscanf和sprintf
- 不确定参数个数的函数的实现
- Android开发---从简单的TextView看Android的控件显示方式
- Hub/Switch/Router工作原理
- 指针和引用的区别?
- 提高PHP运行速度的一些小技巧分享
- winform,DataGridView绑定数据后更改列属性、动态创建(用一个控件显示多表适用)
- 数据库的ACID特定是什么?以及他们分别应用的场合?
- Android如何使用API