Skip to content
This repository has been archived by the owner on Dec 8, 2024. It is now read-only.

Commit

Permalink
Merge pull request #137 from Apoorv012/Network-Gaurdian
Browse files Browse the repository at this point in the history
Network Guardians!
  • Loading branch information
agrim317 authored Dec 17, 2023
2 parents 5f30d78 + ef25e90 commit 5995ae7
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 0 deletions.
19 changes: 19 additions & 0 deletions Problem-setting/Apoorv012/Network-Gaurdians.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# The Network Guardians

Once upon a time, in the digital realm of Cyberlandia, there existed a group of powerful Network Guardians. These guardians were responsible for maintaining the stability and security of the vast network that connected all the inhabitants of Cyberlandia. Among them was the mighty Validator, who ensured the integrity of communication by validating IP addresses.

## The Challenge

One day, the Validator encountered a challenge. A mysterious set of messages was being transmitted across Cyberlandia, and some of them claimed to be from trusted sources, while others seemed dubious. The Validator decided to create a mechanism to filter out the impostors by checking the validity of their claimed IPv4 addresses.

The Validator's challenge was to create a function that would determine whether a given string represented a valid IPv4 address. The function needed to return `true` if the input was a valid IPv4 address and `false` otherwise.

To succeed in this quest, the aspiring programmers had to consider the following rules:

1. An IPv4 address is a string consisting of four numbers separated by dots (e.g., `192.168.1.1`).
2. Each number in the address must be between 0 and 255, inclusive.
3. Numbers should not contain leading zeros, except for the number 0 itself.
4. The dots must be placed correctly, and there should be exactly three dots in the address.
5. No other characters are allowed in the address.

Could you help the Validator with this problem by creating a function that would determine if the given IPv4 address is valid or not? Thanking you in advance and wishing you a good luck for your future endevour.
2 changes: 2 additions & 0 deletions Problem-setting/Apoorv012/explaination.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Explaination:
The provided C++ code defines a function, "isValidIPv4", to determine whether a given string represents a valid IPv4 address. The function employs a while loop to iterate through segments separated by dots, checking for validity based on specific criteria. It ensures that each segment is not empty, doesn't start with a leading zero, and is within the valid range of 0 to 255. The count of valid segments is maintained, and the function returns true only if exactly four valid segments are found, indicating a valid IPv4 address. The main function takes user input for an IPv4 address, calls the validation function, and outputs "true" or "false" accordingly. Note that the code assumes well-formed input following IPv4 address rules but does not handle cases involving leading or trailing spaces.
59 changes: 59 additions & 0 deletions Problem-setting/Apoorv012/solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include <iostream>
#include <string>

int isValidIPv4(std::string ip) {
int count = 0;
int i = 0;
while (i < ip.size()) {
int j = i;

// Find the next dot
while (j < ip.size() && ip[j] != '.') {
j++;
}

// Checks if the number is empty
if (j == i) {
return 0;
}

// Checks if the number starts with 0
if (j - i > 1 && ip[i] == '0') {
return 0;
}

// Checks if the number is greater than 255
int num = 0;
for (int k = i; k < j; k++) {
if (ip[k] < '0' || ip[k] > '9') {
return 0;
}
num = num * 10 + (ip[k] - '0');
}
if (num > 255) {
return 0;
}

// Adds the count upto 4
count++;

// Sets the next index
i = j + 1;
}

// Checks if the count is 4
return count == 4;
}

int main() {
std::string ip;
std::cin >> ip;

if (isValidIPv4(ip)) {
std::cout << "true" << std::endl;
} else {
std::cout << "false" << std::endl;
}

return 0;
}
2 changes: 2 additions & 0 deletions Problem-setting/Apoorv012/test0.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Input - 1: 192.168.1.1
Input - 2: 233.0.540.3
2 changes: 2 additions & 0 deletions Problem-setting/Apoorv012/test0.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Output - 1: true
Output - 2: false

0 comments on commit 5995ae7

Please sign in to comment.