解题思路
#include <stdio.h> #include <stdlib.h> //用atoi()把字符串中的数字转换成int类型的 #include <string.h> //用strcmp()来与"End of file"比较;用strchr()寻找最后一个.的位置 void main() { char IP[31]; int cheak,i; //cheak用来判断字符串是否符合条件 char *str; //声明一个指针来指向.的地址 while (gets(IP)!=NULL&&strcmp(IP,"End of file")) { for (i=1;i<=4;i++) { if (i!=4) { str=strchr( IP ,'.' ); //str指向最后一个.的地址 cheak=atoi(str+1); //(str+1)指向.后面的字符,如果不是数字cheak=0 if (cheak&&cheak>=0&&cheak<=255) *str=' '; //把.更换成' ',让下一次循环指向上一个. else { puts("N"); break; } } else //当i等于4时首字符没有'.'直接检查 { cheak=atoi(IP); if (cheak&&cheak>=0&&cheak<=255) puts("Y"); else puts("N"); } } } }
:
倒叙检查字符串 1.2.3.b 从b开始,再到3,再到2,再到1
注意事项:
参考代码:
0.0分
0 人评分