Write a program to Validate an IPv4 Address.
According to Wikipedia, IPv4 addresses are canonically represented in dot-decimal notation, which consists of four decimal numbers, each ranging from 0 to 255, separated by dots, e.g., 172.16.254.1
Following are steps to check whether a given string is valid IPv4 address or not:
step 1) Parse string with “.” as delimiter using “strtok()” function.
……..a) If ptr contains any character which is not digit then return 0
……..b) Convert “ptr” to decimal number say ‘NUM’
……..c) If NUM is not in range of 0-255 return 0
……..d) If NUM is in range of 0-255 and ptr is non-NULL increment “dot_counter” by 1
……..e) if ptr is NULL goto step 3 else goto step 1
step 3) if dot_counter != 3 return 0 else return 1.
Valid Valid Not valid Not valid
This article is compiled by Narendra Kangralkar. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- C Program to display hostname and IP address
- C Program to find IP Address, Subnet Mask & Default Gateway
- Address of a function in C or C++
- Python | Remove leading zeros from an IP address
- C program to print a string without any quote (singe or double) in the program
- C program to detect tokens in a C program
- Write a URL in a C++ program
- Program to compute Log n
- Program to compare m^n and n^m
- How does a C program executes?
- Output of C++ Program | Set 16
- Output of C++ Program | Set 18
- Output of C++ Program | Set 10
- Output of C Program | Set 29
- Output of C++ Program | Set 14