Validate an IP address in C++

In this post, we will see how to validate an IP address (IPv4) in C++. A valid IPv4 address must be in the form of, 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.

Below is C++ implementation of the same:


Download   Run Code


Valid IP Address

We can also use any of below implementations of isNumber() function:




Also See: Validate IP address in Java (6 methods)

1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)


Thanks for reading.

Please use our online compiler to post code in comments. To contribute, get in touch with us.
Like us? Please spread the word and help us grow. Happy coding 🙂

Leave a Reply

Notify of