面试题整理17 输入一个字符串判断一个字符串是否是有效ip地址

来源:互联网 发布:杀毒清理软件 编辑:程序博客网 时间:2024/05/20 18:46

面试题整理17 输入一个字符串判断一个字符串是否是有效ip地址

标签: 面试题ip字符串问题     http://blog.csdn.net/kuaile123/article/details/21600189
1596人阅读 评论(2)收藏举报
分类:
题目:输入一个字符串判断字符串是否为有效ip地址,


ip地址的形式为XXX.XXX.XXX.XXX。对于XXX表示为0-256的数,但是如果第一位是0而且整数不为0则是非法的,如01

不允许使用strip等函数,只允许使用strlen得到字符串长度

分析:输入字符串长度范围[7,15],以.分段必须能分成四段,每段必须是有效的能转换为整数的字符串,而且转换后整数的值要在[0,255]范围内,并且首位不能为0(除值0外,如00、01、002、013都是不合格的,合格的应该为0、1、2、13),此时可以采用判断字符串长度和整数位数是否相等的方式来进行判断。

代码:自己写了个代码,请大家指正:

[cpp] view plain copy
  1. #include "stdafx.h"  
  2. #include <iostream>  
  3. #include "stdlib.h"  
  4. using namespace std;  
  5.   
  6. //1-3位字符串转换为255以内的整数,如果转换不了则  
  7. bool isValidIpSubInt(char* str);  
  8. //判断是否为有效ip地址,ip地址的形式为XXX.XXX.XXX.XXX  
  9. //对于XXX表示为0-256的数,但是如果第一位是0而且整数不为0则是非法的,如01  
  10.   
  11. bool  isValidIpAddress(char* str)  
  12. {  
  13.     if(str == NULL)  
  14.         return false;  
  15.     int length = strlen(str);  
  16.     if(length < 7 || length > 15) //最  
  17.         return false;  
  18.     int subNum = 1;  
  19.     int startIndex = 0;  
  20.     int endIndex = 0;  
  21.     while(startIndex < length)  
  22.     {  
  23.         if(subNum > 4)  
  24.             return false;  
  25.         while(str[endIndex] <= '9' && str[endIndex] >= '0')  
  26.             ++endIndex;  
  27.         if(endIndex-startIndex > 3 || endIndex-startIndex == 0)  
  28.             return false;  
  29.         if(str[endIndex] == '.'|| subNum == 4)  
  30.         {  
  31.             ++subNum;  
  32.             char* tempStr = new char[endIndex-startIndex+1];  
  33.             for(int i=0; i< endIndex-startIndex; ++i)  
  34.                 tempStr[i] = str[startIndex+i];  
  35.             tempStr[endIndex-startIndex] = '\0';  
  36.             bool isValid = isValidIpSubInt(tempStr);  
  37.             delete[] tempStr;  
  38.             if( isValid == false)  
  39.             {  
  40.                 return false;  
  41.             }  
  42.         }  
  43.         else{  
  44.             break;  
  45.         }  
  46.         startIndex = ++endIndex;  
  47.     }  
  48.     if( subNum == 5 || startIndex > length)  
  49.     {  
  50.         return true;  
  51.     }else{  
  52.         return false;  
  53.     }  
  54.   
  55. }  
  56. //1-3位字符串转换为255以内的整数,如果转换不成功则返回false  
  57. //具体判断方法,如果转换后的整数不在0-255之间的,则返回false  
  58. //001此类的认为是非法的,所以需要判断转换后整数的位数和字符串的长度是否一致,如果不一致则返回false  
  59. bool isValidIpSubInt(char* str)  
  60. {  
  61.     if(str == NULL || strlen(str) == 0 )  
  62.         return false;  
  63.     int length = strlen(str);  
  64.     int result = 0;  
  65.     while(*str != '\0')  
  66.     {  
  67.         if(*str < '0' || *str >'9')  
  68.         {  
  69.             return false;  
  70.         }  
  71.         result = result*10 + *str-'0';  
  72.         ++str;  
  73.     }  
  74.     if(result <= 255 && result >= 0 && *str == '\0')  
  75.     {  
  76.         int resultLength = 1;  
  77.         int tempResult = result;  
  78.         while( tempResult /=10 )  
  79.             ++resultLength;  
  80.         if(resultLength != length)  
  81.             return false;  
  82.     }else{  
  83.         return false;  
  84.     }  
  85.     return true;  
  86. }  
  87. void Test(char* testName,char* testString,bool expectedResult)  
  88. {  
  89.     cout << "test: " << testName << endl;  
  90.     cout << "String is :" ;  
  91.     if(testString == NULL)  
  92.     {  
  93.         cout << "NULL" << endl;  
  94.     }else  
  95.     {  
  96.         cout << testString << endl;  
  97.     }  
  98.     bool result = isValidIpAddress(testString);  
  99.     //cout << " the result is :" << result << endl;  
  100.     //cout << "expected result is :" << expectedResult << endl;  
  101.     cout << testName << " ";  
  102.     if( result == expectedResult)  
  103.     {  
  104.         cout << "passed !" << endl;  
  105.     }  
  106.     else   
  107.     {  
  108.         cout << "failed !" << endl;  
  109.     }  
  110. }  
  111. //测试空串  
  112. void Test1()  
  113. {  
  114.     char* str = NULL;  
  115.     Test("Test1",str,false);  
  116. }  
  117. //测试超过256的串  
  118. void Test2()  
  119. {  
  120.     char* str = "255.255.256.255";  
  121.     Test("Test2",str,false);  
  122. }  
  123. //测试没有有效输入的串  
  124. void Test3()  
  125. {  
  126.     char* str = "1.B.C.D";  
  127.     Test("Tes3",str,false);  
  128. }  
  129. //测试前面是0的串  
  130. void Test4()  
  131. {  
  132.     char* str = "01.0.0.0";  
  133.     Test("Test4",str,false);  
  134. }  
  135. //测试正确最长串  
  136. void Test5()  
  137. {  
  138.     char* str = "255.255.255.255";  
  139.     Test("Test4",str,true);  
  140. }  
  141. //测试正确最短串  
  142. void Test6()  
  143. {  
  144.     char* str = "0.0.0.0";  
  145.     Test("Test6",str,true);  
  146. }  
  147. int main()  
  148. {  
  149.     Test1();  
  150.     Test2();  
  151.     Test3();  
  152.     Test4();  
  153.     Test5();  
  154.     Test6();  
  155.     system("pause");  
  156. }
0 0
原创粉丝点击