Skip to content
Programmingoneonone
Programmingoneonone
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
  • Work with US
Programmingoneonone
Programmingoneonone

HackerEarth Sahil computer address problem solution

YASH PAL, 31 July 202411 February 2026
In this HackerEarth Sahil’s computer address problem solution we have Given a String S, you need to report if the given String is a valid IP Address :
A valid IP address is:
  1. It has exactly 4 non-empty parts separated by 3 dots, like: 255 [dot] 255 [dot] 255 [dot] 255
  2. The decimal value of each part of the IP address should never exceed 255 and never be less than zero.
[dot] is written, instead of the .(dot) character for readability.
HackerEarth Sahil's computer address problem solution

HackerEarth Sahil’s computer address problem solution.

#include<bits/stdc++.h>
using namespace std;

int main()
{
int dots = 0, i = 0;
string s, octet_str;
cin >> s;

bool is_valid = true;
while (1)
{
char c = s[i++];
if (c == '.' || c == 0)
{
if (octet_str.empty())
{
is_valid = false;
break;
}

int octet_value = stoi(octet_str);
if (octet_value < 0 || octet_value > 255)
{
is_valid = false;
break;
}

if (c == 0)
break;

++dots;
octet_str.clear();
}
else
{
octet_str += c;
}
}

if (dots != 3)
{
is_valid = false;
}

cout << (is_valid ? "YESn" : "NOn");
return 0;
}
coding problems solutions HackerEarth HackerEarth

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes