C++判断是否是IP地址

判断是否是IP地址

bool isIPAddress(const char *s)
{
    const char *pChar;
    bool rv = true;
    int tmp1, tmp2, tmp3, tmp4, i;

    while( 1 )
    {
        i = sscanf(s, "%d.%d.%d.%d", &tmp1, &tmp2, &tmp3, &tmp4);

        if( i != 4 )
        {
            rv = false;
            break;
        }

        if( (tmp1 > 255) || (tmp2 > 255) || (tmp3 > 255) || (tmp4 > 255) )
        {
            rv = false;
            break;
        }

        for( pChar = s; *pChar != 0; pChar++ )
        {
            if( (*pChar != '.')
                && ((*pChar < '0') || (*pChar > '9')) )
            {
                rv = false;
                break;
            }
        }
        break;
    }

    return rv;
}


 

你可能感兴趣的:(C++)