C++ Solution AC. Any advice on code quality/readability?


  • 0
    A

    Any advice on how can I make it better?

    bool isValidNum(const char* start,const char* end)
    {
        int num=0;
    
        while(start!=end)
        {
            num=num*10 + ((*start)-'0');
            start++;
        }
        if(num>=0 && num<=255) 
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    bool isHexAlphaNum(const char* const ptr)
    {
        return ((((*ptr)>='A' && ((*ptr)<='F')) || ((*ptr)>='a' && ((*ptr)<='f')))||isdigit(*ptr));
    }
    bool isHexAlpha(const char* const ptr)
    {
        return (((*ptr)>='A' && ((*ptr)<='F')) || ((*ptr)>='a' && ((*ptr)<='f')));
    }
    
    string validIPAddress(string IP) {
        const char *iter=&IP[0];
        const char *startPtr=&IP[0];
        bool isIP4=false;
        bool isIP6=false;
        bool isAlpha=false;
        bool isNum=false;
        int groupCounter=0;
        int charCounter=0;
        
        while(true)
        {
            if(*iter=='\0')
            {
                if((isIP4 || charCounter>4 || (!isAlpha && !isNum) || iter==startPtr || groupCounter==8) && isIP6)
                {
                    return "Neither";
                }
                if((isIP6|| isAlpha || iter==startPtr || charCounter>3  || (*startPtr=='0'&& charCounter>1) ||groupCounter==4)&&isIP4)
                {
                    return "Neither";
                }
                if(isIP4 && charCounter!=1)
                {
                    if(!isValidNum(startPtr,iter))
                    {
                        return "Neither";
                    }
                }
                groupCounter++;
                break;
            }
            else if(*iter==':')
            {
                isIP6=true;
                if(isIP4 || charCounter>4 || (!isAlpha && !isNum) || iter==startPtr || groupCounter==8)
                {                                        
                    return "Neither";
                }
                isAlpha=false;
                isNum=false;
                charCounter=0;
                startPtr=iter+1;
                groupCounter++;
            }
            else if(*iter=='.')
            {
                isIP4=true;
                if(isIP6|| isAlpha ||!isNum || iter==startPtr || charCounter>3 || ((*startPtr)=='0' && (charCounter>1 || groupCounter<1)) || groupCounter==4)
                {
                    return "Neither";
                }
                if(charCounter!=1 && !isValidNum(startPtr,iter))
                {
                    break;
                }
                isAlpha=false;
                isNum=false;
                charCounter=0;
                startPtr=iter+1;
                groupCounter++;
            }
            else
            {
                if(!isHexAlphaNum(iter))
                {
                    break;
                }
                if(!isAlpha) isAlpha=isHexAlpha(iter);
                if(!isNum) isNum=isdigit(*iter);
    
                charCounter++;
            }
            iter++;
        }
        if(isIP4 && groupCounter==4)
        {
            return "IPv4";
        }
        if(isIP6 && groupCounter==8)
        {
            return "IPv6";
        }
        return "Neither";
        
    }

  • 0
    Z

    Did you mean shorter? or faster?


Log in to reply
 

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