Skip to content
Programmingoneonone
Programmingoneonone

Learn everything about programming

  • Home
  • 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
Programmingoneonone
Programmingoneonone

Learn everything about programming

HackerEarth Sahil computer address problem solution

YASH PAL, 31 July 2024
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

Post navigation

Previous post
Next post

Are you a student and stuck with your career or worried about real-time things, and don't know how to manage your learning phase? Which profession to choose? and how to learn new things according to your goal, and land a dream job. Then this might help to you.

Hi My name is YASH PAL, founder of this Blog and a Senior Software engineer with 5+ years of Industry experience. I personally helped 40+ students to make a clear goal in their professional lives. Just book a one-on-one personal call with me for 30 minutes for 300 Rupees. Ask all your doubts and questions related to your career to set a clear roadmap for your professional life.

Book session - https://wa.me/qr/JQ2LAS7AASE2M1

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

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