C# - simple if else solution - easy to understand


  • 0
    D
    public string ValidIPAddress(string IP)
            {
                string result = "IPv4";
                string result2 = "IPv6";
                string result3 = "Neither";
    
                {
                    if (string.IsNullOrEmpty(IP))
                    {
                        return result3;
                    }
                }
    
                {
                    string[] address = IP.Split('.');
    
                    //ip v4
                    if(address.Length == 4)
                    {
                        try
                        {
                            foreach (string s in address)
                            {
                                int temp = int.Parse(s);
    
                                if (temp < 0) return result3;
    
                                if(temp == 0)
                                {
                                    if (s.Length > 1)
                                        return result3;
                                }
    
                                if (temp>0 &&(int)Math.Log10(temp) != s.Length - 1)
                                {
                                    return result3;
                                }
    
                                
    
                                if(temp > 255)
                                {
                                    return result3;
                                }
                            }
    
                            return result;
                        }
                        catch (Exception)
                        {
                            // exception of int parse
                            return result3;
                        }
                    }
    
                }
    
                {
                    string[] address = IP.Split(':');
    
                    if (address.Length == 8)
                    {
    
                        foreach (string s in address)
                        {
                            if (s.Length > 4)
                            {
                                return result3;
                            }
    
                            if (s.Length == 0)
                                return result3;
    
                            string temp = s.ToLower();
                            for (int i = 0; i < temp.Length; i++)
                            {
                                char c = temp[i];
    
                                if (c - '0' >= 0 && c - '0' <= 9)
                                {
                                    continue;
                                }
                                else if (c - 'a' >= 0 && c - 'a' <= 5)
                                {
                                    continue;
                                }
                                else
                                {
                                    return result3;
                                }
                            }
    
                        }
    
                        return result2;
    
                    }
    
                 }
    
                return result3;
            }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.