编写一个函数来验证字符串是否是有效的IPv4或IPv6地址,输出结果,测试执行结果,各项边界检查,C语言实现,代码效率高,笔试题

详细题目描述如下图:

编写一个函数来验证字符串是否是有效的IPv4或IPv6地址,输出结果,测试执行结果,各项边界检查,C语言实现,代码效率高,笔试题_第1张图片

代码:

#include 
#include 
#include 
 
char* validIPAddress(char* IP)
{
    int flag = 0;
    int len = 0;
    int num = 0;
    int Count = 0;
    int first = 0;
    int i = 0,j=0;

    if(IP == NULL)
    {
        return "Neither";
    }

    len = strlen(IP);
    if (strchr(IP,'.') != NULL)  // 含 .的情况,判断是否是IPv4
    {
        for(i = 0;i <= len-1; i++)
        {
            if(IP[i] >= '0' && IP[i] <= '9')
            {
                num = num * 10 + IP[i] - '0';
            }
            else if(IP[i] == '.')
            {
                if((i == 0)||(i == len-1))
                {
                    break;
                }

                if(IP[i-1] < '0' || IP[i-1] > '9')
                {
                    break;
                }

                if(IP[i] == '.')
                {
                    Count++;
                    if(Count > 3)
                    {
                        break;
                    }
                }

                if(num > 255)
                {
                    break;
                }

                if(num != 0 && IP[first] == '0')
                {
                    break;
                }
                first = i+1;
                num = 0;
            }
            else
            {
                break;
            }
        }

        if(Count == 3)
        {
            flag = 1;
        }
    }
    else if (strchr(IP,':') != NULL) // 含 :的情况,判断是否是IPv6
    {
        for(i = 0;i <= len-1; i++)
        {
            if((IP[i] >= '0' && IP[i] <= '9')||(tolower(IP[i]) >= 'a' && tolower(IP[i]) <= 'f'))
            {
                j++;
                if (j > 4)
                {
                    break;
                }
            }
            else if(IP[i] == ':')
            {
                if((i == 0)||(i == len-1))
                {
                    break;
                }
                if (j == 0)
                {
                    break;
                }
                Count++;
                if(Count > 7)
                {
                    break;
                }
                j = 0;
            }
            else
            {
                break;
            }
        }

        if((Count == 7)&&(j <= 4))
        {
            flag = 2;
        }
    }

    if (flag == 1)
    {
        return "IPv4";
    }
    else if (flag == 2)
    {
        return "IPv6";
    }
    else
    {
        return "Neither";
    }
}

int main(void)
{
    printf("%s\n",validIPAddress("172.16.254.1"));
    printf("%s\n",validIPAddress("172.016.254.1"));
    printf("%s\n",validIPAddress("172.16.254."));
    printf("%s\n",validIPAddress("172..254.1"));
    printf("%s\n",validIPAddress("256.256.256.256"));
    printf("%s\n",validIPAddress("2001:0db8:95a3:0:0:8A2E:0370:7334"));
    printf("%s\n",validIPAddress("02001:0db8:95a3:0:0:8A2E:0370:7334"));
    printf("%s\n",validIPAddress("2001:0db8:95a3::0:8A2E:0370:7334"));
    printf("%s\n",validIPAddress("2001:0db8:95a3:0:0:8A2E:0370:"));
    printf("%s\n",validIPAddress("2001:0db8:95a3:0:0:8A2E:0370:02345"));

    return 0;
}

 

测试结果:

IPv4

Neither

Neither

Neither

Neither

IPv6

Neither

Neither

Neither

Neither

你可能感兴趣的:(C/C++,VisualStudio,网络,算法,字符串,c语言,数据结构,ipv)