Question

In: Computer Science

Plase, write program in c++. IP address consists of four integers of range [0, 255] separated...

Plase, write program in c++.
IP address consists of four integers of range [0, 255] separated by dots.
The next three rows show three correct IP-address:
130.0.0.0
193.197.0.01
255.00.255.255

Write a program that determines whether a given string is a valid IP-address.

outpot should be 1 if given IP address is valid, or 0 otherwise.

Solutions

Expert Solution

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........


Related Solutions

write a c++ program in which for loop iterates over all integers in the range 0...
write a c++ program in which for loop iterates over all integers in the range 0 to 99 inclusive and prints out all of those numbers dividible by both 2 and 5.
The range of integers that can be represented by this 12-bit construction is -255 to 255...
The range of integers that can be represented by this 12-bit construction is -255 to 255 (a 9-bit signed integer has a range of -256 to 255). What are the floating-point representations for the first eight, odd, positive, decimal integers (i.e., 1, 3, 5, 7, 9, 11, 13, 15) in this 12-bit notation?
Write a Console Java program that inserts 25 random integers in the range of 0 to...
Write a Console Java program that inserts 25 random integers in the range of 0 to 100 into a Linked List. (Use SecureRandom class from java.security package. SecureRandom rand = new SecureRandom(); - creates the random number object rand.nextInt(100) - generates random integers in the 0 to 100 range) Using a ListItreator output the contents of the LinkedList in the reverse order. Using a ListItreator output the contents of the LinkedList in the original order.
Write a Console Java program that inserts 25 random integers in the range of 0 to...
Write a Console Java program that inserts 25 random integers in the range of 0 to 100 into a Linked List. (Use SecureRandom class from java.security package. SecureRandom rand = new SecureRandom(); - creates the random number object rand.nextInt(100) - generates random integers in the 0 to 100 range) Using a ListItreator output the contents of the LinkedList in the original order. Using a ListItreator output the contents of the LinkedList in the reverse order.
The question: Write a program in Python that writes four random integers in range 1-100 on...
The question: Write a program in Python that writes four random integers in range 1-100 on a file named 'num.txt'. Write try-except block to handle at least two standard python error (any two errors). Hints: import random def main(): # Local variables # Open output file. # Write random numbers to the file. # Write it on to the file. # Close the file. # Call the main function. main() Here is my answer: please let me know if anything...
Directions: Write a C++ program that will create an array of four integers. It will allow...
Directions: Write a C++ program that will create an array of four integers. It will allow the user to enter in four valid scores and store them in the array. ( valid range is 0 - 100) It will compute the letter grade based upon the four scores, namely, A = 90 - 100, B= 80- 89, C = 70-79, D = 60-69, otherwise F. It will display the scores and letter grade to the screen. NOTE: No menu is...
Write a C++ program that inputs three integers in the range [1..13] that represent a hand...
Write a C++ program that inputs three integers in the range [1..13] that represent a hand of three cards. Your program should output an English evaluation of the hand. In the output, cards will be described as follows: - 2–10 are described by the words for their numeric values: two, three, etc. - 1 is called an ace, 11 is called a jack, 12 is called a queen, and 13 is called a king. The evaluation of a hand is...
C# Create a console application that asks the user for two numbers in the range 0-255...
C# Create a console application that asks the user for two numbers in the range 0-255 and then divides the first number by the second: Enter a number between 0 and 255: 100 Enter another number between 0 and 255: 8 100 divided by 8 is 12 Enter a number between 0 and 255: apples Enter another number between 0 and 255: bananas FormatException: Input string was not in a correct format.
Create 512 subnets from Class A IP address range 10.*.*.*. For the first four (4) and...
Create 512 subnets from Class A IP address range 10.*.*.*. For the first four (4) and last four (4) subnets, list the network IP address, broadcast IP address, 1st assignable IP address, and last assignable IP address. What subnet mask IP address will all devices in all subnets use?
For example My IP address 128. 0. 0. 1 My IP address 53. 20. 62. 201...
For example My IP address 128. 0. 0. 1 My IP address 53. 20. 62. 201 My IP address 77. 122. 180. 76 My IP address 54. 33. 11. 11 My IP address 33. 33. 2. 11 My IP address 202. 29. 205. 69 My IP address 215. 18. 15. 185 My IP address 56. 66. 33. 30 How do you write a regular expression that covers all IP addresses and display all list using a loop in Python
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT