IPV4地址可以用一个32位无符号整数来表示,一般用点分方式来显示,点将IP地址分成4个部分,每个部分为8位,表示成一个无符号整数(因此正号不需要出现),如10.137.17.1,是我们非常熟悉的IP地址,一个IP地址串中没有空格出现(因为要表示成一个32数字)。
现在需要你用程序来判断IP是否合法。
数据范围:数据组数:
进阶:时间复杂度:
,空间复杂度:%5C)
IPV4地址可以用一个32位无符号整数来表示,一般用点分方式来显示,点将IP地址分成4个部分,每个部分为8位,表示成一个无符号整数(因此正号不需要出现),如10.137.17.1,是我们非常熟悉的IP地址,一个IP地址串中没有空格出现(因为要表示成一个32数字)。
输入一个ip地址,保证不包含空格
返回判断的结果YES or NO
255.255.255.1000
NO
#include <stdio.h>
int main() {
short num_d = -1;//某一个组
char dout_count = 0;//点的数量
while (1) {
char c = getchar();
if (c == '\0' || c == '\n' || c == '\b')
{
// 出来时判断最后一组数字
if (num_d < 0 || num_d > 255 || dout_count != 3) {
puts("NO");
return 0;
}
break;//字符串末尾
}
if ((c != '.') && ((c > '9') || (c < '0'))) {
puts("NO");
return 0;
}
else if (c == '.') {
// 是 .
if (num_d < 0 || num_d > 255) {
puts("NO");
return 0;
}
dout_count++;
num_d = -1;
}
else {
// 是数字
if (num_d == -1) num_d = c - '0';
else
{
if (num_d == 0) {//当已经是0的时候不能还有数字在后面
puts("NO");
return 0;
}
num_d = (num_d * 10) + (c) - '0';
}
if (num_d > 255) {
puts("NO");
return 0;
}
}
}
puts("YES");
return 0;
} 验证了特殊符号之类的,没想到没有特殊符号,空间和时间尽量压缩了。
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
char str[20] = {'\0'} ;
char t_str[10] = {'\0'};
int subscript = 0;
int n = 0;
bool format = true;
bool num_range = true;
int nn = 0;
gets(str);
//检查格式是否正确
for (int i = 0 ; i < strlen(str) ; i++) {
if (str[i] == '.') {
//遇到'.'则前后都要是数组
if (!(str[i - 1] >= '0' && str[i - 1] <= '9' && str[i + 1] >= '0' &&
str[i + 1] <= '9')) {
format = false;
}
//遇到'.0'且后一位在1到9之间
else if (str[i] == '.' && str[i + 1] == '0') {
if (str[i + 2] > '0' && str[i + 2] <= '9') {
format = false;
}
}
}//遇到'0'且后一位在1到9之间
else if (str[i] == '0' && i == 0) {
if (str[i + 1] > '0' && str[i + 1] <= '9') {
format = false;
}
}
else if (str[i] < '0' || str[i] > '9') {
format = false;
}
}
//判断数字范围是否正确
for (int i = 0 ; i < strlen(str) + 1; i++ ) {
if (str[i] == '.' || str[i] == '\0') {
nn++;
n = atoi(t_str);
if (n < 0 || n > 255) {
num_range = false;
break;
}
n = 0;
memset(t_str, '\0', 10);
subscript = 0;
} else if (str[i] >= '0' && str[i] <= '9') {
t_str[subscript++] = str[i];
}
}
//printf("%d %d %d\n", format, num_range, nn);
if (format == true && num_range == true && nn == 4) printf("YES\n");
else printf("NO\n");
return 0;
} #include <stdio.h>
int main()
{
char str[100]={0};
int ip[10]={0};
while(scanf("%s",str)!=EOF)
{
int flag=1;
if(str[0]=='0' && str[1]!='.') flag=0; //第一段数字前带0
int i=0;
int count=0;
while(i<strlen(str))
{
if(str[i]=='.') //记录'.'字符出现的次数
{
count++;
}
//数字前带0的情况
if(str[i]=='.' && str[i+1]=='0' && str[i+2]!='.' && str[i+2]!='\0')
{
flag=0;
break;
}
//输入只能含有数字和字符'.'
if(!(str[i]>='0' && str[i]<='9' || str[i]=='.'))
{
flag=0;
break;
}
i++;
}
if(count!=3) flag=0; //只能含有三个字符'.'
//记录字符串中数字的段数
int n=sscanf(str,"%d.%d.%d.%d",&ip[0],&ip[1],&ip[2],&ip[3]);
if(n==4) //段数为4
{
for(int i=0;i<4;i++)
{
if(ip[i]<0 || ip[i]>255)
{
flag=0;
break;
}
}
}
else flag=0;
if(flag==1) printf("YES\n");
else if(flag==0) printf("NO\n");
}
return 0;
} #include <string.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
int cnt = 0;
int flag = 1;
char s[100] = {0};
char ipStr[10] = {0};
int bitIpStr = 0;
int temp = 0;
scanf("%s", s);
for(int i = 0; i < strlen(s); i++){
if(s[i] >= '0' && s[i] <= '9'){
ipStr[bitIpStr++] = s[i];
}else if(s[i] == '.' && bitIpStr > 0){
if(bitIpStr >= 2 && ipStr[0] == '0'){
flag = 0;
break;
}
ipStr[bitIpStr++] = 0;
temp = atoi(ipStr);
if(temp > 255){
flag = 0;
break;
}
bitIpStr = 0;
cnt++;
}else{
flag = 0;
break;
}
}
if(bitIpStr > 0){
ipStr[bitIpStr++] = 0;
temp = atoi(ipStr);
if(temp <= 255 && bitIpStr > 0 && !(bitIpStr >= 3 && ipStr[0] == '0')){
cnt++;
}
}
if(cnt == 4 && flag == 1){
printf("YES\n");
}else{
printf("NO\n");
}
return 0;
} #include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
char buf[20];
scanf("%s",buf);
int dot = 0;
char *p=buf, *last=buf;
char tmp[5];
while(*p != '\0') {
if((*p) - '0' >= 0 && (*p) -'0' <= 9 || (*p) == '.'){
if(*p == '.') {
if(dot >= 3) {
goto err;
}
dot++;
if((p-last)/sizeof(char) > 3)
goto err;
if(p == last)
goto err;
if(*last == '0' && (p-last)/sizeof(char) != 1) {
// puts("ignore 0\n");
goto err;
}
strncpy(tmp,last,(p-last)/sizeof(char));
tmp[(p-last)/sizeof(char)] = '\0';
if(atoi(tmp) > 255) {
// printf("%d\n",atoi(tmp));
// puts(">255");
goto err;
}
last = p+1;
}
p++;
} else {
// puts("illegal char\n");
goto err;
}
}
if(*last == '0' && (p-last)/sizeof(char) != 1) {
// puts("ignore 0\n");
goto err;
}
if((p - last + 1)/sizeof(char) <= 1) {
// puts("4th sector error\n");
goto err;
}
strncpy(tmp,last,(p-last)/sizeof(char));
tmp[(p-last)/sizeof(char)] = '\0';
if(atoi(tmp) > 255) {
// printf("%d\n",atoi(tmp));
// puts(">255");
goto err;
}
if(dot != 3){
// puts("dot != 3\n");
goto err;
}
printf("YES");
return 0;
err:
printf("NO\n");
exit(0);
} #define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
int main() {
char str1[40] = { 0 };
char str[40] = { 0 };
int i = 0;
while (scanf("%s", str1) != EOF) {
if (strlen(str1) < 7) {
printf("NO\n");
return 0;
}
if (str1[0] == '0' && isdigit(str1[1]) || !isdigit(str1[0])) {
printf("NO\n");
return 0;
}
for (int i = 1; i < strlen(str1); i++) {
if (!isdigit(str1[i]) && str1[i] != '.') {
printf("NO\n");
return 0;
}
if (str1[i] == '0' && isdigit(str1[i + 1]) && str1[i - 1] == '.') {
printf("NO");
return 0;
}
}
char* p = NULL;
for (p = strtok(str1, "."); p != NULL; p = strtok(NULL, ".")) {
i++;
int num = atoi(p);
if (num > 256 || num < 0) {
printf("NO\n");
return 0;
}
}
}
if (i > 4) {
printf("NO\n");
return 0;
}
printf("YES\n");
return 0;
}
一开始过了二十多个用例,后面一直调试调试,终于全部通过了。 #include <stdio.h>
#include <string.h>
#define MAX 19
int main() {
char ip[MAX];
scanf("%s", ip);
int ip_len = strlen(ip);
int flag = 0;//合法标志位,用来判断用‘.’间隔的4个数字部分的每个部分是否合法,合法flag就加一,4个部分都合法(即flag==4),才会输出“YES”
for (int i=0; i<ip_len; i++) {
if (i==0 || ip[i-1]=='.') {//表示字符串字符数值部分开始
int num = 0;//记录把字符串字符数值转换为整型数值的值
int count = 0;//记录字符串中数字部分的长度
if (ip[i]>='0' && ip[i]<='9') {
//不满足下面while的循环条件表示字符串字符数值部分的结束
while (ip[i+count] != '.' && (i+count) != ip_len) {
count++;
}
if (count > 3) {//长度大于3表示不合法,直接结束循环
break;
}
else {
/*长度是2或者3时,并且第一个字符数值为“0”时,
表明不合法,直接结束循环*/
if (count > 1 && ip[i] == '0') {
break;
}
for (int j=i; j<count+i; j++) {//把字符数值转换为整型数值
int num_j = ip[j] - '0';
num *= 10;
num += num_j;
}
if (num>255) {//数值大于255不合法,结束循环
break;
}
else {//合法情况,flag加一
flag++;
}
}
}
else {//是非数值字符,表明不合法,结束循环
break;
}
}
}
if (flag == 4) {
printf("YES\n");
}
else {
printf("NO\n");
}
return 0;
} //目前38个测试用例全部通过了
#include <stdio.h>
#include <string.h>
int main()
{
int i = 1;
int flag = 0;
char ch,str[20] = {0};
char *ptr,c = '.';
str[0] = '.';
while ((ch = getchar()) != '\n')
{
str[i++] = ch;
}
if (i > 16 || i < 8)
{
printf("NO");
return 0;
}
while(ptr=strrchr(str,c))
{
//针对01.1.2.8 1.01.3.4等情况的特殊处理
if (atoi(ptr+1) > 255 || atoi(ptr + 1) < 0 ||
(strlen(ptr+1)) > 1 && (9 > atoi(ptr+1) > 0))
{
printf("NO");
return 0;
}
*ptr='\0';
flag++;
}
if (flag != 4)
{
printf("NO");
return 0;
}
printf("YES");
return 0;
} /*
思路:分别对250-255, 200-249, 100-199, 10-99, 0-9 匹配得出数字部分
加上小数点,重复三次
最后再补上一次
*/
#include <stdio.h>
#include <regex.h>
int main(void)
{
char in[50];
regex_t reg;
char* pattern = "^((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$";
int status;
int ret;
ret = regcomp(®, pattern, REG_EXTENDED | REG_NOSUB);
while(scanf("%s", in) != EOF){
status = regexec(®, in, 0, 0, 0);
if( 0 == status){
printf("YES\n");
}else{
printf("NO\n");
}
}
regfree(®);
return 0;
} 练习一下对字符串进行操作
#include<stdio.h>
// 拆分字符串
int split(char in[50], char **sub) {
char *p = in;
char key[2] = ".";
int num = 0;
p = strtok(in, key);
while(p != NULL) {
sub[num++] = p;
p = strtok(NULL, key);
}
return num++;
}
// 判断每组数字是否合法
int isOK(char IP[10]) {
int a = atoi(IP);
if(a < 0 || a > 255){
return 0;
}
return 1;
}
int main() {
char in[50] = {0};
while(gets(in)) {
char *sub[10];
int num = split(in, sub);
int ret = 1;
for(int i = 0; i < num; i++) {
ret &= isOK(sub[i]);
}
if(ret == 1) {
printf("YES\n");
} else {
printf("NO\n");
}
}
}