当前位置:网站首页>Niuke network: verify the IP address

Niuke network: verify the IP address

2022-06-21 17:20:00 lsgoose

  There are two ways .

Catalog

1. Task extraction

2. Regular expressions


1. Task extraction

No matter ipv4 still ipv6, Our task is to split the string first , Then judge whether each string is legal .

therefore , We first write a function that splits a string with a certain character .

For the split string , To judge whether ipv4:

1. The number of strings should be 4

2. Each string must be 0-255 Between

3. Longer than 1 The first bit of the string of cannot be 0

To judge whether ipv6:

1. The number of strings should be 8

2. The characters in each string are either numbers , Or a-f,A-F The characters in

3. The length of each string cannot exceed 4

The code is as follows :

class Solution {
public:
    /**
     *  verification IP Address 
     * @param IP string character string   One IP Address string 
     * @return string character string 
     */
    const string IPv6="IPv6";
    const string IPv4="IPv4";
    vector<string> split(string s, string spliter){
        vector<string> res;
        int i;
        while((i=s.find(spliter))&&i!=s.npos){
            res.push_back(s.substr(0,i));
            s=s.substr(i+1);
        }
        res.push_back(s);
        return res;
    }
    bool isIPv4(string &IP){
        vector<string> s=split(IP, ".");
        if(s.size()!=4){
            return false;
        }
        for(int i=0;i<s.size();++i){
            if(s[i].size()==0){
                return false;
            }
            if(s[i].size()>3 || (s[i].size()>0 && s[i][0]=='0')){
                return false;
            }
            for(int j=0;j<s[i].size();++j){
                if(!isdigit(s[i][j])){
                    return false;
                }
            }
            int num=stoi(s[i]);
            if(num<0 || num>255){
                return false;
            }
        }
        return true;
    }
    bool isIPv6(string &IP){
        vector<string> s=split(IP, ":");
        if(s.size()!=8){
            return false;
        }
        for(int i=0;i<s.size();++i){
            if(s[i].size()==0 || s[i].size()>4){
                return false;
            }
            for(int j=0;j<s[i].size();++j){
                if(!(isdigit(s[i][j]) || (s[i][j] >= 'a' && s[i][j] <= 'f') || (s[i][j] >= 'A' && s[i][j] <= 'F')))
                    return false;
            }
        }
        return true;
    }
    
    string solve(string IP) {
        if(IP.size()==0){
            return "Neither";
        }else if(isIPv4(IP)){
            return IPv4;
        }else if(isIPv6(IP)){
            return IPv6;
        }else{
            return "Neither";
        }
    }
};

2. Regular expressions

The patterns here can be matched with regular expressions :

class Solution {
public:
    /**
     *  verification IP Address 
     * @param IP string character string   One IP Address string 
     * @return string character string 
     */
    const string IPv6="IPv6";
    const string IPv4="IPv4";
    vector<string> split(string s, string spliter){
        vector<string> res;
        int i;
        while((i=s.find(spliter))&&i!=s.npos){
            res.push_back(s.substr(0,i));
            s=s.substr(i+1);
        }
        res.push_back(s);
        return res;
    }
    bool isIPv4(string &IP){
        vector<string> s=split(IP, ".");
        if(s.size()!=4){
            return false;
        }
        for(int i=0;i<s.size();++i){
            if(s[i].size()==0){
                return false;
            }
            if(s[i].size()>3 || (s[i].size()>0 && s[i][0]=='0')){
                return false;
            }
            for(int j=0;j<s[i].size();++j){
                if(!isdigit(s[i][j])){
                    return false;
                }
            }
            int num=stoi(s[i]);
            if(num<0 || num>255){
                return false;
            }
        }
        return true;
    }
    bool isIPv6(string &IP){
        vector<string> s=split(IP, ":");
        if(s.size()!=8){
            return false;
        }
        for(int i=0;i<s.size();++i){
            if(s[i].size()==0 || s[i].size()>4){
                return false;
            }
            for(int j=0;j<s[i].size();++j){
                if(!(isdigit(s[i][j]) || (s[i][j] >= 'a' && s[i][j] <= 'f') || (s[i][j] >= 'A' && s[i][j] <= 'F')))
                    return false;
            }
        }
        return true;
    }
    
    string solve(string IP) {
        if(IP.size()==0){
            return "Neither";
        }else if(isIPv4(IP)){
            return IPv4;
        }else if(isIPv6(IP)){
            return IPv6;
        }else{
            return "Neither";
        }
    }
};

原网站

版权声明
本文为[lsgoose]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/172/202206211405238005.html