打印

判断一个字符串是否ip地址(包含ipv4和ipv6)

[复制链接]
248|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
keer_zu|  楼主 | 2023-9-15 11:04 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式



代码如下:
#include <iostream>
#include <algorithm>
using namespace std;

class Solution
{
public:
    string validIPAddress(string IP)
    {
        //以.和:来区分ipv4和ipv6
        for (int i = 0; i < IP.length(); i++)
        {
            if (IP[i] == '.')
                return isIPv4(IP) ? "IPv4" : "Neither";
            else if (IP[i] == ':')
                return isIPv6(IP) ? "IPv6" : "Neither";
        }
        return "Neither";
    }
private:
    bool isIPv4(string IP)
    {
        int dotcnt = 0;
        //数一共有几个.
        for (int i = 0; i < IP.length(); i++)
        {
            if (IP[i] == '.')
                dotcnt++;
        }
        //ipv4地址一定有3个点
        if (dotcnt != 3)
            return false;
        string temp = "";
        for (int i = 0; i < IP.length(); i++)
        {
            if (IP[i] != '.')
                temp += IP[i];
            //被.分割的每部分一定是数字0-255的数字
            if (IP[i] == '.' || i == IP.length() - 1)
            {
                if (temp.length() == 0 || temp.length() > 3)
                    return false;
                for (int j = 0; j < temp.length(); j++)
                {
                    if (!isdigit(temp[j]))
                        return false;
                }
                int tempInt = stoi(temp);
                if (tempInt > 255 || tempInt < 0)
                    return false;
                string convertString = to_string(tempInt);
                if (convertString != temp)
                    return false;
                temp = "";
            }
        }
        if (IP[IP.length()-1] == '.')
            return false;
        return true;
    }
    bool isIPv6(string IP) {
        int dotcnt = 0;
        for (int i = 0; i < IP.length(); i++)
        {
            if(IP[i] == ':')
                dotcnt++;
        }
        if (dotcnt != 7) return false;
        string temp = "";
        for (int i = 0; i < IP.length(); i++)
        {
            if (IP[i] != ':')
                temp += IP[i];
            if (IP[i] == ':' || i == IP.length() - 1)
            {
                if (temp.length() == 0 || temp.length() > 4)
                    return false;
                for (int j = 0; j < temp.length(); j++)
                {
                    if (!(isdigit(temp[j]) ||(temp[j] >= 'a' && temp[j] <= 'f') || (temp[j] >= 'A' && temp[j] <= 'F')))
                        return false;
                }
                temp = "";
            }
        }
        if (IP[IP.length()-1] == ':')
            return false;
        return true;
    }
};

int main()
{
    Solution sol;
    string s;
    while(1){
        cout << "Please input a string: " << endl;
            cin >> s;
        if(s == "exit"){
                break;
        }
            cout << sol.validIPAddress(s) << endl;
    }
    cout << "bye" << endl;
    return 0;
}


运行结果:


使用特权

评论回复

相关帖子

发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:qq群:49734243 Email:zukeqiang@gmail.com

1314

主题

12271

帖子

53

粉丝