In: Computer Science
Code:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
bool checkNumber(const string& str){// check if the given
string is a numeric string or not
return !str.empty() &&
(str.find_first_not_of("[0123456789]") == std::string::npos);
}
// Here function to split the string using given delimiter
vector<string> split(const string& str, char
delim){
auto i = 0;
vector<string> list;
auto position = str.find(delim);
while (position != string::npos){
list.push_back(str.substr(i, position - i));
i = ++position;
position = str.find(delim, position);
}
list.push_back(str.substr(i, str.length()));
return list;
}
// Function validate the given 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 the string is number, positive, and range
if (!checkNumber(str) || stoi(str) < 0 || stoi(str) >
255)
return false;
}
return true;
}
int main(){
cout<<"Enter the IP Address::";
string ip;
cin>>ip;
if (validateIP(ip))//call the function
cout <<endl<< "1";
else
cout <<endl<< "0";
return 0;
}
Note:***I hope your happy with my answer****If you have any doubts please comment me*****Thank you........