Header Ads Widget

Sahil's computer address

Problem:-

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. 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.
Input Format :
The first and only line of input contains the string S. The string shall consist of only numbers and .(dot) symbol of ASCII character set only. All IP addresses will be in decimal i.e. base 10 notation only.
Output Format :
Print the "YES" or "NO" answer on a single line.
Constraints :
1≤|S|≤64
SAMPLE INPUT
 
255.255.255.0
SAMPLE OUTPUT
 
YES
Explanation
The string contains exactly 4 non-empty parts and 3 dots. The value of each part is <=255 and >= 0.
Time Limit:1.0 sec(s) for each input file.
Memory Limit:64 MB
Source Limit:1024 KB

solution:-

#include<stdio.h>


#include<stdlib.h>
#include<string.h>
void main()
{
char s[65],a[3];
int i,k=0,x,flag=0,l;
scanf("%s",s);
l=strlen(s);
for(i=0;s[i]!='\0';i++)
{
if(s[i]=='.')
{
x=atoi(a);
if(flag==0)
if(x>255||x<0||s[i+1]=='.')
{
printf("NO");
flag=1;
            }
}
else
{
a[k]=s[i];
k++;
}
if(k>2)
k=0;
}
x=atoi(a);
if(flag==0)
{
if(x>255||x<0||s[l-1]=='.')
printf("NO");
else
printf("YES");}
}


Post a Comment

0 Comments