想要使用 C 语言验证 IPv4 地址,但在点数方面出现错误
Want validate IPv4 address using C language but getting error in number of dots
这是我的代码
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
int main()
{
int alp=0;
int ch, spaces;
char str[30];
int er=0;
int dot = 3;
int cl;
int i, j, k, l;
int x;
int y;
int z;
int p;
printf("Enter IPV4 IP address:");//Enter the IP address
scanf("%s",&str);//For taking input
char *part;
char *part1 = strtok(str, ".");
char *s1 = part1;
char *part2 = strtok(NULL, ".");
char *s2 = part2;
char *part3 = strtok(NULL, ".");
char *s3 = part3;
char *part4 = strtok(NULL, ".");
char *s4 = part4;
if(p < 0 || p > 255)//To check the range of the ip address
{
er++;
printf("IP Address not in range");
}
if (part4[0]=='0')//to check if the 1st position holds zero
{
er++;
printf("\nZero Found. IP address should not containt leading zero");
}
if(dot != 3)//to check the separaters
{
dot--;
printf("\nValid");
}
else
{
dot++;
printf("\nLimit exceeded!! number of dots more than three");
}
return 0;
}
所以我得到的错误是//如果我输入 192.168.1.1(有效 IP)显示错误限制,则检查我的代码的分隔符部分如果我输入更多点的 IP 显示相同的错误,也会超出限制.
我的那部分代码需要帮助。
首先使用正常和不稳定的 IP 号尝试我的代码并观察其输出。然后将您的代码与我的代码进行比较并检测您的错误。
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
int dotCount(const char* str) {
int dots = 0;
while(*str != NULL) {
if(*str++ == '.') dots++;
}
return dots;
}
int main()
{
char str[30];
int er=0;
int dot = 0;
printf("Enter IPV4 IP address:");//Enter the IP address
scanf("%s",str);//For taking input
puts(str);
// Dot count check
dot = dotCount(str);
if(dot == 3)
{
printf("\nDot count ok");
}
else
{
er++;
printf("\nLimit exceeded!! number of dots more than three: %d\n", dot);
}
char *part;
part = strtok(str, ".");
while(part != NULL) {
dot++;
// printf("part: %s\n", part); // Part debug
if(strlen(part) > 1 && *part == '0') {
er++;
printf("\nZero Found. IP address should not containt leading zero: %s\n", part);
}
int range = atoi(part);
if(range < 0 || range > 255)//To check the range of the ip address
{
er++;
puts("IP Address not in range\n");
}
part = strtok(NULL, ".");
}
if(er > 0) {
printf("%d errors found\n",er);
}
else {
puts("No errors foundi IP adress is ok\n");
}
return 0;
}
这是我的代码
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
int main()
{
int alp=0;
int ch, spaces;
char str[30];
int er=0;
int dot = 3;
int cl;
int i, j, k, l;
int x;
int y;
int z;
int p;
printf("Enter IPV4 IP address:");//Enter the IP address
scanf("%s",&str);//For taking input
char *part;
char *part1 = strtok(str, ".");
char *s1 = part1;
char *part2 = strtok(NULL, ".");
char *s2 = part2;
char *part3 = strtok(NULL, ".");
char *s3 = part3;
char *part4 = strtok(NULL, ".");
char *s4 = part4;
if(p < 0 || p > 255)//To check the range of the ip address
{
er++;
printf("IP Address not in range");
}
if (part4[0]=='0')//to check if the 1st position holds zero
{
er++;
printf("\nZero Found. IP address should not containt leading zero");
}
if(dot != 3)//to check the separaters
{
dot--;
printf("\nValid");
}
else
{
dot++;
printf("\nLimit exceeded!! number of dots more than three");
}
return 0;
}
所以我得到的错误是//如果我输入 192.168.1.1(有效 IP)显示错误限制,则检查我的代码的分隔符部分如果我输入更多点的 IP 显示相同的错误,也会超出限制.
我的那部分代码需要帮助。
首先使用正常和不稳定的 IP 号尝试我的代码并观察其输出。然后将您的代码与我的代码进行比较并检测您的错误。
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
int dotCount(const char* str) {
int dots = 0;
while(*str != NULL) {
if(*str++ == '.') dots++;
}
return dots;
}
int main()
{
char str[30];
int er=0;
int dot = 0;
printf("Enter IPV4 IP address:");//Enter the IP address
scanf("%s",str);//For taking input
puts(str);
// Dot count check
dot = dotCount(str);
if(dot == 3)
{
printf("\nDot count ok");
}
else
{
er++;
printf("\nLimit exceeded!! number of dots more than three: %d\n", dot);
}
char *part;
part = strtok(str, ".");
while(part != NULL) {
dot++;
// printf("part: %s\n", part); // Part debug
if(strlen(part) > 1 && *part == '0') {
er++;
printf("\nZero Found. IP address should not containt leading zero: %s\n", part);
}
int range = atoi(part);
if(range < 0 || range > 255)//To check the range of the ip address
{
er++;
puts("IP Address not in range\n");
}
part = strtok(NULL, ".");
}
if(er > 0) {
printf("%d errors found\n",er);
}
else {
puts("No errors foundi IP adress is ok\n");
}
return 0;
}