Write a C program to check given string is valid IPv4 address or not

By | December 3, 2016

C program to check string is valid IPv4 address or not


Write a C program to check given string is valid IPv4 address or not.This program will read an IP address in String and check whether it is a valid IPv4 address or not.

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


STEPS : :


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.

e.g. ptr = strtok(str, DELIM);

step 2)
……..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.


Below is the source code of the C program to check given string is valid IPv4 address or not which is successfully compiled and run on the windows system.The Output of the program is shown below.


SOURCE : :



Also Read : : Write a C program to check date is valid or not


OUTPUT : : 


Leave a Reply