Home > Mobile >  Want validate IPv4 address using C language but getting error in number of dots
Want validate IPv4 address using C language but getting error in number of dots

Time:09-30

here is my code

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
int main()
{
    int alp=0;
    int ch, spaces;
    char str[30];
    int er=0;
    int dot = 3;
    int cl;
    int i, j, k, l;
    int x;
    int y;
    int z;
    int p;
    printf("Enter IPV4 IP address:");//Enter the IP address
    scanf("%s",&str);//For taking input
    char *part;
    char *part1 = strtok(str, ".");
    char *s1 = part1;
    char *part2 = strtok(NULL, ".");
    char *s2 = part2;
    char *part3 = strtok(NULL, ".");
    char *s3 = part3;
    char *part4 = strtok(NULL, ".");
    char *s4 = part4;
    
    if(p < 0 || p > 255)//To check the range of the ip address
    {
       er  ; 
        printf("IP Address not in range");
    }
    if (part4[0]=='0')//to check if the 1st position holds zero
    {
        er  ;
        printf("\nZero Found. IP address should not containt leading zero");
        
    }
    if(dot != 3)//to check the separaters
    {
        dot--;
        printf("\nValid");
    }
    else
    {
        dot  ;
        printf("\nLimit exceeded!! number of dots more than three");
    }
    
    return 0;
}

So the error I'm getting is in //to check the separators part of my code if I enter 192.168.1.1(valid IP) showing error Limit exceed also if I enter IP with more dots showing the same error.

Want help in that part of my code.

CodePudding user response:

First try my code with normal and erratical IP numbers and observe its outputs. Then compare your code with mine and detect your errors.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>

int dotCount(const char* str) {
    int dots = 0;
    while(*str != NULL) {
        if(*str   == '.') dots  ;
    }
    return dots;
}

int main()
{
    char str[30];
    int er=0;
    int dot = 0;
    printf("Enter IPV4 IP address:");//Enter the IP address
    scanf("%s",str);//For taking input
    puts(str);

    // Dot count check
    dot = dotCount(str);
    if(dot == 3)
    {
        printf("\nDot count ok");
    }
    else
    {
        er  ;
        printf("\nLimit exceeded!! number of dots more than three: %d\n", dot);
    }

    char *part;
    part = strtok(str, ".");
    while(part != NULL) {
        dot  ;
//      printf("part: %s\n", part); // Part debug
        if(strlen(part) > 1 && *part == '0') {
            er  ;
            printf("\nZero Found. IP address should not containt leading zero: %s\n", part);
        }
        int range = atoi(part);
        if(range < 0 || range > 255)//To check the range of the ip address
        {
            er  ;
            puts("IP Address not in range\n");
        }
        part = strtok(NULL, ".");
    }

    if(er > 0) {
        printf("%d errors found\n",er);
    }
    else {
        puts("No errors foundi IP adress is ok\n");
    }

    return 0;
}
  • Related