Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

#include <stdio.

h>
#include <stdlib.h>
#include<string.h>
#include<string>
#include<iostream>
#define DELIM "."
using namespace std;
int t;
/* return 1 if string contain only digits, else return 0 */
int valid_digit(char *ip_str)
{
while (*ip_str) {
if (*ip_str >= '0' && *ip_str <= '9')
++ip_str;
else
return 0;
}
return 1;
}
/* return 1 if IP string is valid, else return 0 */
int is_valid_ip(char *ip_str)
{
int i, num, dots = 0;
char *ptr;
if (ip_str == NULL)
return 0;
// See following link for strtok()
// http://pubs.opengroup.org/onlinepubs/009695399/functions/strtok_r.html
ptr = strtok(ip_str, DELIM);
t=atoi(ptr);
if (ptr == NULL)
return 0;
while (ptr) {
/* after parsing string, it must contain only digits */
if (!valid_digit(ptr))
return 0;
num = atoi(ptr);
/* check for valid IP */
if (num >= 0 && num <= 255) {
/* parse remaining string */
ptr = strtok(NULL, DELIM);
if (ptr != NULL)
++dots;
} else
return 0;
}
/* valid IP string must contain 3 dots */
if (dots != 3)

return 0;
return 1;
}
// Driver program to test above functions
int main()
{
char* str=new char[100];
cout<<"Enter string\n";
cin>>str;
if(is_valid_ip(str)==1)
{
if(t>=1&&t<=127)
{cout<<"It is Valid
}
if(t>=128&&t<=191)
{cout<<"It is Valid
}
if(t>=192&&t<=223)
{cout<<"It is Valid
}
if(t>=224&&t<=239)
{cout<<"It is Valid
}
if(t>=240&&t<=255)
{cout<<"It is Valid
}
}
else
cout<<"It is Invalid";
return 0;
}

and belong to class A";


and belong to class B";
and belong to class C";
and belong to class D";
and belong to class E";

You might also like