Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Validate IP Address in C++
This article is serving the purpose of validating the correct IP (internet protocol) address by virtue of C++ code programming. The IP address is a 32-bit dot-decimal-notation, broken into four decimal numbers segments ranging from 0 to 255. Furthermore, these numbers are separated by dots consecutively. The IP address serves the purpose of identifying a host machine in the network in a unique manner in order to establish a connection among them.
So, in order to validate the correct IP address input from the user-end, the following algorithm briefs how exactly the code sequence is materialized to identify the correct IP address as follows;
Algorithm
START Step-1: Input the IP address Step-2: Spilt the IP into four segments and store in an array Step-3: Check whether it numeric or not using Step-4: Traverse the array list using foreach loop Step-5: Check its range (below 256) and data format Step-6: Call the validate method in the Main() END
Thence, as per the algorithm the following c++ drafted to validate the IP address, in which couple of essentials functions are being employed to determine the numeric form, range, and splitting the input data respectively;
Example
#include <iostream>
#include <vector>
#include <string>
using namespace std;
// check if the given string is a numeric string or not
bool chkNumber(const string& str){
return !str.empty() &&
(str.find_first_not_of("[0123456789]") == std::string::npos);
}
// Function to split string str using 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> slist = split(ip, '.');
// if token size is not equal to four
if (slist.size() != 4)
return false;
for (string str : slist){
// check that string is number, positive, and range
if (!chkNumber(str) || stoi(str) < 0 || stoi(str) > 255)
return false;
}
return true;
}
// Validate an IP address in C++
int main(){
cout<<"Enter the IP Address::";
string ip;
cin>>ip;
if (validateIP(ip))
cout <<endl<< "***It is a Valid IP Address***";
else
cout <<endl<< "***Invalid IP Address***";
return 0;
}
After the compilation of the above code using a standard c++ editor, the following output is being produced which is duly checking whether the input number 10.10.10.2 is a correct IP address or not as follows;
Output
Enter the IP Address:: 10.10.10.2 ***It is a Valid IP Assress***