清楚ip地址的规则:
1.3个点
2.4个数据,每个数据是1~3位。正则表达式[0-9]{1,3}.[0-9]{1,3}.[0-9{1,3}.[0-9]{1,3}
3.每个数据n,0=<n<=255
#include<stdio.h>
#include<stdlib.h>
int checkip(char *ip)
{
int ifip=0;
if(NULL==ip)return ifip;
int pointnumber=0;
int charnumber=0;
int num=0;
while(*ip!='\0')
{
if(*ip>='0'&&*ip<='9')
{
num=num*10+(*ip-'0');
charnumber++;
}
else if(*ip=='.'&&num>=0&&num<=255&&charnumber>=1&&charnumber<=3)
{
num=0;charnumber=0;pointnumber++;
printf("%d",pointnumber);
}
else
{
ifip=0;
break;
}
ip++;
}
if(pointnumber==3&&num>=0&&num<=255&&charnumber>=1&&charnumber<=3)
ifip=1;
return ifip;
}
int main()
{
char a[100];
scanf("%s",a);
if(checkip(a)==1)
printf("valid\n");
else
printf("invalid\n");
return 0;
}