
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
C Program to validate an IP address
In this program we will see how to validate an IP address using C. The IPv4 addresses are represented in dot-decimal notation. There are four decimal numbers (all are ranging from 0 to 255). These four numbers are separated by three dots.
An example of a valid IP is: 192.168.4.1
To validate the IP address we should follow these steps
Tokenize the string (IP address) using the dot “.” delimiter
If the sub strings are containing any non-numeric character, then return false
If the number in each token is not in range 0 to 255, then return false
If there are exactly three dots and four parts then it is a valid IP address
Example Code
#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 validate_ip(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 ip1[] = "192.168.4.1"; char ip2[] = "172.16.253.1"; char ip3[] = "192.800.100.1"; char ip4[] = "125.512.100.abc"; validate_ip(ip1)? printf("Valid
"): printf("Not valid
"); validate_ip(ip2)? printf("Valid
"): printf("Not valid
"); validate_ip(ip3)? printf("Valid
"): printf("Not valid
"); validate_ip(ip4)? printf("Valid
"): printf("Not valid
"); }
Output
Valid Valid Not valid Not valid
- Related Articles
- Validate IP Address in C#
- Validate IP Address in C++
- Validate IP Address in Python
- How to validate the IP address using PowerShell?
- How to validate an email address in C#?
- Program to display hostname and IP address C
- C# program to Display Hostname and IP address
- C Program to display hostname and IP address
- Python program to validate email address
- Program to convert IP address to hexadecimal in C++
- C# program to find IP Address of the client
- How to hide an IP address
- Python program to remove leading zeros from an IP address
- Python program to validate postal address format
- JavaScript program to retrieve clients IP address

Advertisements