Validate an IP address in C++
This post will discuss how to validate an IP address (IPv4) in C++. A valid IPv4 address must be in the form of xxx.xxx.xxx.xxx, where xxx is a number from 0-255.
For example,
'14.8.9.28', '255.255.255.255', '1.0.0.9' are valid IP addresses and
'100.xyz.1.15', '115.300.10.60', '50.35.6' are invalid IP addresses.
The idea is to split the given IP address to tokens using dot(.) as a delimiter. If the token size is not equal to 4, we return false; otherwise, we validate each token. If the token is not a number or the numbers are not in the valid range between 0 and 255, we return false. If all tokens are valid, we return true.
Following is a C++ implementation of the same:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
#include <iostream> #include <vector> #include <string> using namespace std; // check if a given string is a numeric string or not bool isNumber(const string &str) { // `std::find_first_not_of` searches the string for the first character // that does not match any of the characters specified in its arguments return !str.empty() && (str.find_first_not_of("[0123456789]") == std::string::npos); } // Function to split string `str` using a given delimiter vector<string> split(const string &str, char delim) { auto i = 0; vector<string> list; auto pos = str.find(delim); while (pos != string::npos) { list.push_back(str.substr(i, pos - i)); i = ++pos; pos = str.find(delim, pos); } list.push_back(str.substr(i, str.length())); return list; } // Function to validate an IP address bool validateIP(string ip) { // split the string into tokens vector<string> list = split(ip, '.'); // if the token size is not equal to four if (list.size() != 4) { return false; } // validate each token for (string str: list) { // verify that the string is a number or not, and the numbers // are in the valid range if (!isNumber(str) || stoi(str) > 255 || stoi(str) < 0) { return false; } } return true; } // Validate an IP address in C++ int main() { string ip = "14.8.9.28"; // string ip = "100.xyz.1.15"; // string ip = "115.300.10.60"; if (validateIP(ip)) { cout << "Valid IP Address" << endl; } else { cout << "Invalid IP Address" << endl; } return 0; } |
Output:
Valid IP Address
We can also use any of the following implementations of the isNumber() function:
|
1 2 3 4 5 6 7 8 9 10 11 12 |
bool isNumber(const string &str) { // iterate over the string until we find a non-digit character for (char ch: str) { if (!isdigit(ch)) { return false; } ] return true; } |
|
1 2 3 4 5 |
bool isNumber(const string &str) { return !str.empty() && find_if (str.begin(), str.end(), [](char c) { return !isdigit(c); }) == str.end(); } |
That’s all about validating an IP address in C++.
Also See:
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)