菜鸟求助 C语言编程判断IP MAC地址是否合法

2024-11-24 00:20:28
推荐回答(1个)
回答1:

#include

int checkIP(const char* p)
{
int n[4];
char c[4];
if (sscanf(p, "%d%c%d%c%d%c%d%c",
&n[0], &c[0], &n[1], &c[1],
&n[2], &c[2], &n[3], &c[3])
== 7)
{
int i;
for(i = 0; i < 3; ++i)
if (c[i] != '.')
return 0;
for(i = 0; i < 4; ++i)
if (n[i] > 255 || n[i] < 0)
return 0;
return 1;
} else
return 0;
}

int main()
{
const char* x[] =
{
"192.168.1.1",
"10.0.0.1.",
"127.256.0.1",
"iugerjiogjioe",
"172.16,0.1",
0
};
const char* m[] =
{
"不是合法的IP地址",
"是合法的IP地址"
};
int i = 0;

while(x[i] != 0) {
printf("%s %s\n", x[i], m[checkIP(x[i])]);
++i;
}
return 0;
}