Tuesday, 7 February 2017

C program to check whether a given IP is valid or not

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
main()
{
    int num,dots=0;
char ip[50],*p,*end;
    gets(ip);
    p=strtok(ip,".");
while(p != NULL)
   {
        num=strtol(p,&end,10);
        if(!*end)
        {
         if(num >=0 && num <= 255)
              {
                           p=strtok(NULL,".");
                             if(p != NULL)
                                         dots++;
              }
              else
             {
printf("not valid ip: number not in range");
                         exit(0);
             }
      }
      else
     {
                         printf("not valid ip: not a digit");
                          exit(0);
      }
   }
   if(dots != 3)
printf("not a valid ip: not enough dots");
   else
               printf("valid ip");
getch();
}

No comments:

Post a Comment