Write a C programming which can read the text file that contain 4 integers of an
ID: 3830679 • Letter: W
Question
Write a C programming which can read the text file that contain 4 integers of an IP address along with a component to store up to 10 characters. Example of data file.txt look like: 123.89.72.45 water 147.12.5.74 salt 169.85.64.56 bottle 123.89.4.6 bag Then from that file, write code that can report the first 2 integers of the IP address associated with its name and save it as a file, as the following example: 123.89 water bag 147.12 salt 169.85 bottle Also, write code to arrange the name of IP address as alphabetical order, and save it as text file example: bag 123.89.4.6 bottle 169.85.64.56 salt 147.12.5.74 water 123.89.72.45Explanation / Answer
Good question from you i have a small code similar to your question that will helps you.
Program to validate an IP address:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define DELIM "."
int valid_digit(char *ip_str)
{
while (*ip_str) {
if (*ip_str >= '0' && *ip_str <= '9')
++ip_str;
else
return 0;
}
return 1;
}
int is_valid_ip(char *ip_str)
{
int i, num, dots = 0;
char *ptr;
if (ip_str == NULL)
return 0;
ptr = strtok(ip_str, DELIM);
if (ptr == NULL)
return 0;
while (ptr) {
if (!valid_digit(ptr))
return 0;
num = atoi(ptr);
if (num >= 0 && num <= 255) {
/* parse remaining string */
ptr = strtok(NULL, DELIM);
if (ptr != NULL)
++dots;
} else
return 0;
}
if (dots != 3)
return 0;
return 1;
}
int main()
{
char ip1[] = "128.0.0.1";
char ip2[] = "125.16.100.1";
char ip3[] = "125.512.100.1";
char ip4[] = "125.512.100.abc";
is_valid_ip(ip1)? printf("Valid "): printf("Not valid ");
is_valid_ip(ip2)? printf("Valid "): printf("Not valid ");
is_valid_ip(ip3)? printf("Valid "): printf("Not valid ");
is_valid_ip(ip4)? printf("Valid "): printf("Not valid ");
return 0;
}
Sample Output:
Valid
Valid
For sortings strings alphabetical order we have:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.