Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write A C program Objectives Familiarize the student with: The preprocessor Prin

ID: 3912787 • Letter: W

Question

Write A C program

Objectives


Familiarize the student with:
The preprocessor
Printing on screen
Functions
Function calls with parameters
Calling functions with parameters
Function return values
Printing on screen


Scenario
Write a function that checks whether or not a given string is a valid IP address (in human-readable form, of course). This function
should return 1 if the address is valid, and 0 if not. Your function should check if:
there are 4 parts in the string, separated by dots;
each part contains only digits;
each number is in the range of 0 to 255, inclusive.
For converting string fragments to integer values, you can use the strtol, atoi or sscanf functions. Separate the declaration of the
function from its full definition. Write a second function that calls the first one and then prints an appropriate message: "127.0.0.1 is a
valid IP address" or "a.b.c.d is not a valid IP address". Write a code to test it with values - call the second function.
You need to define all the constants
(even one-character constants!) with preprocessor defines. You should also give them appriopriate names.


#include <stdio.h>
/* your code */
int main()
{
/* your code */
return 0;
}
/* your code */


Example output
127.0.0.1 is a valid IP address
127.0.01 is not a valid IP address
127.0..1 is not a valid IP address
127.zero.0.1 is not a valid IP address
127.297.0.1 is not a valid IP address
127.2555.0.1 is not a valid IP address

Explanation / Answer

PROGRAM:

#include <stdio.h>

#include<string.h>

#include <stdlib.h>

/* Function declarations */

void printMessage(char ipaddr[]);

int isValidIp(char ipaddr[]);

/*This function prints whether the given IP address is valid or not*/

void printMessage(char ipaddr[])

{

int ans=isValidIp(ipaddr);

/*Printing whether the IP address is valid or not*/

(ans==1)?printf("%s is a valid IP address ",ipaddr):printf("%s is not a valid IP address ",ipaddr);

}

/*This function validates the IP address*/

int isValidIp(char ipaddr[])

{

/* An temporary char array to store the IP address*/

char temp_arr[strlen(ipaddr)];

/*Copying the IP address into the temp_arr array*/

strcpy(temp_arr, ipaddr);

/*An variable for counting number of octets in the ip address*/

int count=0;

/*An character pointer to store the IP address splitted by dots */

char * octet;

/*strtok splits an string into tokens based on the given delimiter */

octet = strtok (temp_arr,".");

/*Iterating over the octets array*/

while (octet != NULL)

{

/*Converting the octet into integer*/

int octet_value=atoi(octet);

/*Validating the octet converted from string to int */

if(octet_value==0 && octet[0]!='0') return 0;

if(octet_value<0 || octet_value>255) return 0;

/*Updating the value of octet count , if an octet is valid*/

else count+=1;

/*Getting the next octet*/

octet = strtok (NULL, ".");

}

/*If the number of octets are 4 ,then given IP address is valid */

if(count==4) return 1;

/*Else , the given IP address is not valid*/

return 0;

}

int main()

{

/*An char array to store the IP address*/

char ipAddress[16];

printf("Enter the IP address to be validated: ");

scanf("%s",ipAddress);

/*Calling the printMessage function*/

printMessage(ipAddress);

return 0;

}

Sample Input and Output:

Enter the IP address to be validated: 127.0.0.1

127.0.0.1 is a valid IP address

Enter the IP address to be validated: 127.zero.0.1

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote