यह लेख C++ कोड प्रोग्रामिंग के आधार पर सही IP (इंटरनेट प्रोटोकॉल) पते को मान्य करने के उद्देश्य को पूरा कर रहा है। आईपी एड्रेस एक 32-बिट डॉट-दशमलव-नोटेशन है, जिसे 0 से 255 तक के चार दशमलव संख्या खंडों में विभाजित किया गया है। इसके अलावा, इन नंबरों को लगातार बायडॉट्स से अलग किया जाता है। आईपी एड्रेस नेटवर्क में एक होस्ट मशीन को उनके बीच एक कनेक्शन स्थापित करने के लिए एक अद्वितीय तरीके से पहचानने के उद्देश्य से कार्य करता है।
इसलिए, उपयोगकर्ता के अंत से सही आईपी एड्रेस इनपुट को मान्य करने के लिए, निम्नलिखित एल्गोरिथम संक्षेप में बताता है कि सही आईपी पते की पहचान करने के लिए कोड अनुक्रम को वास्तव में कैसे कार्यान्वित किया जाता है;
एल्गोरिदम
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में मान्य विधि को कॉल करें
तब से, एल्गोरिथम के अनुसार निम्नलिखित c++ आईपी पते को मान्य करने के लिए तैयार किया गया है, जिसमें क्रमशः संख्यात्मक रूप, श्रेणी और इनपुट डेटा को विभाजित करने के लिए कुछ आवश्यक कार्यों को नियोजित किया जा रहा है;
उदाहरण
#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;
} एक मानक c++ संपादक का उपयोग करके उपरोक्त कोड के संकलन के बाद, निम्न आउटपुट तैयार किया जा रहा है जो विधिवत जांच कर रहा है कि इनपुट नंबर 10.10.10.2 एक सही आईपी पता है या नहीं;
आउटपुट
Enter the IP Address:: 10.10.10.2 ***It is a Valid IP Assress***