#include <stdio.h>
#include <string.h>
int main(void)
{
char str[31],temp[31];
int a,b,c,d;
printf("输入 IP 地址:");
gets(str);
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) // sscanf() 的作用:从一个字符串中读进与指定格式相符的数据.
{
printf("%d %d %d %d \n",a,b,c,d);
sprintf(temp, "%d.%d.%d.%d",a,b,c,d); //把格式化的数据写入字符串temp
printf("%s\n",temp);
printf("%s",str);
if(strcmp(temp,str)==0)
{
printf("YES\n");
}
else
{
printf("NO\n");
}
}
else
{
printf("NO\n");
}
return 0;
}
判断IP地址(精辟)
最新推荐文章于 2024-04-07 10:12:58 发布