Validate an IP address

Validate an IP address (IPv4). A valid IPv4 address must be in the form of XXX.XXX.XXX.XXX, where XXX is a number from 0-255.


For example,

‘’, ‘’, ‘’ are valid IP addresses and
‘’, ‘115.300.10.60’, ‘50.35.6’ are invalid IP addresses.

The idea is to split the given IP address into tokens by using dot(.) as a delimiter. If the token size is not equal to 4, we return false else we validate each token. If token is not a number or the numbers are not in the valid range between 0 to 255, we return false. If all tokens are valid, we return true.

C++ implementation –

Download   Run Code


Valid IP Address


Thanks for reading.

Please use ideone or C++ Shell or any other online compiler link to post code in comments.
Like us? Please spread the word and help us grow. Happy coding 🙂

Leave a Reply

Notify of