判斷字符串是否是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