判断字符串是否是IP地址


 

#include <stdio.h>
#include <string.h>

bool isIP(const char* str);

int main()
{
char str[] = "111.111.111.21";
char str2[] = "a.111.111.111";
char str3[] = "11.1.1.1.d";

printf("%d\n",isIP(str));
printf("%d\n",isIP(str2));
printf("%d\n",isIP(str3));
}


bool isIP(const char* str)
{
int a,b,c,d;
char temp[100];
if((sscanf(str,"%d.%d.%d.%d",&a,&b,&c,&d))!=4)
return false;
sprintf(temp,"%d.%d.%d.%d",a,b,c,d);
if(strcmp(temp,str) != 0)
return false;
if(!((a <= 255 && a >= 0)&&(b <= 255 && b >= 0)&&(c <= 255 && c >= 0)))
return false;
else
return true;
}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM