Question

In: Computer Science

A valid IPv4 address is of the form ###.###.###.###, where each number, separated by '.', is...

A valid IPv4 address is of the form ###.###.###.###, where each number, separated by '.', is 8 bits [0, 255]. Create program that validates input string containing an IP address. Use C programming language.

- Remove any newline \n from input string

- The input prompt should say "Enter IP Address of your choosing: "

- If IP address is invalid, it should print "You entered invalid IP\n"

- If IP address is valid, it should print "You entered valid IP\n"

- Convert string to integer using atof

Example:

• Valid IP

    Enter IP Address of your choosing: 192.169.0.4

    You entered valid IP

• Invalid IP

    Enter IP Address of your choosing: 192.666.432.1122

    You entered invalid IP

Solutions

Expert Solution

//Compiled using gcc compiler

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int validate_number(char *str) {
   while (*str) {
      if(!isdigit(*str)){ //if the character is not a number, return false
    
         return 0;
      }
      str++; //point to next character
   }
   return 1;
}
int validateip(char *ip) { //check whether the IP is valid or not
   int i, num, dots = 0;
   char *ptr;
   if (ip == NULL)
      return 0;
      ptr = strtok(ip, "."); //cut the string using dor delimiter
      if (ptr == NULL)
         return 0;
   while (ptr) {
      if (!validate_number(ptr)) //check whether the sub string is holding only number or not
         return 0;
         num = atoi(ptr); //convert substring to number
         if (num >= 0 && num <= 255) {
            ptr = strtok(NULL, "."); //cut the next part of the string
            if (ptr != NULL)
               dots++; //increase the dot count
         } else
            return 0;
    }
    if (dots != 3) //if the number of dots are not 3, return false
       return 0;
      return 1;
}
int main() {
   char ip[50],n;
   printf("Enter the ip address:");
   scanf("%s",ip);
   n=validateip(ip);
   if(n==1)
      printf("You entered valid IP\n");
   else
      printf("You entered invalid IP\n");

   }

OUTPUT


Related Solutions

What are the differences between IPv4 and IPv6? What is the format of an IPv4 address?  
What are the differences between IPv4 and IPv6? What is the format of an IPv4 address?  
In java: A complex number is a number in the form a + bi, where a...
In java: A complex number is a number in the form a + bi, where a and b are real numbers and i is sqrt( -1). The numbers a and b are known as the real part and imaginary part of the complex number, respectively. You can perform addition, subtraction, multiplication, and division for complex numbers using the following formulas: a + bi + c + di = (a + c) + (b + d)i a + bi - (c...
Discuss the problem of IPv4 Address exhaustion and the measures that were taken to mitigate that...
Discuss the problem of IPv4 Address exhaustion and the measures that were taken to mitigate that issue.
Discuss the problem of IPv4 Address exhaustion and the measures that were taken to mitigate that...
Discuss the problem of IPv4 Address exhaustion and the measures that were taken to mitigate that issue.
JAVA Programming A complex number is a number in the form a + bi, where a...
JAVA Programming A complex number is a number in the form a + bi, where a and b are real numbers and i is sqrt( -1). The numbers a and b are known as the real part and imaginary part of the complex number, respectively. You can perform addition, subtraction, multiplication, and division for complex numbers using the following formulas: a + bi + c + di = (a + c) + (b + d)i a + bi - (c...
IP Address: 37.124.67.99 Subnet Mask: 255.240.0.0 find Network address, Broadcast address, first valid host address, last...
IP Address: 37.124.67.99 Subnet Mask: 255.240.0.0 find Network address, Broadcast address, first valid host address, last valid host address and number of valid hosts in the network. show very clear steps for all the calculations.
Assignment Write a network diagnostics bash script that does the following: Finds the IPv4 address of...
Assignment Write a network diagnostics bash script that does the following: Finds the IPv4 address of the default gateway on your machine (one way to get this is the 'default' entry, also shown as 0.0.0.0, in the output of 'ip route') and runs a ping (with a count of 5 pings) to it. Runs another count of 5 pings to the site example.com. Outputs a 1-line summary of interfaces on your machine, not including the loopback address (lo). Outputs a...
Why are the Class D and Class E IPv4 address ranges not available for general use?...
Why are the Class D and Class E IPv4 address ranges not available for general use? What is the difference between a port and a socket?
Convert this into Chomsky normal form, where each rule is in the form: A --> BC...
Convert this into Chomsky normal form, where each rule is in the form: A --> BC or A --> a A --> A + B | B B --> B x C | C C --> (A) | 5
Translate the following into standard form and symbolic form. Is it a valid or invalid argument?...
Translate the following into standard form and symbolic form. Is it a valid or invalid argument? Sound or unsound? Why? "Science and religion are basically the same. In religion, you believe something based purely upon faith. But in science, even if there is evidence, you have to have faith in your experiments and in the scientific method."" critical thinking a concise guide 4th edition, its not showing up on chegg study
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT