您的位置:首页 > 职场人生

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

2014-03-20 10:47 633 查看
题目:输入一个字符串判断字符串是否为有效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),此时可以采用判断字符串长度和整数位数是否相等的方式来进行判断。

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

#include "stdafx.h"
#include <iostream>
#include "stdlib.h"
using namespace std;

//1-3位字符串转换为255以内的整数,如果转换不了则
bool isValidIpSubInt(char* str);
//判断是否为有效ip地址,ip地址的形式为XXX.XXX.XXX.XXX
//对于XXX表示为0-256的数,但是如果第一位是0而且整数不为0则是非法的,如01

bool  isValidIpAddress(char* str)
{
if(str == NULL)
return false;
int length = strlen(str);
if(length < 7 || length > 15) //最
return false;
int subNum = 1;
int startIndex = 0;
int endIndex = 0;
while(startIndex < length)
{
if(subNum > 4)
return false;
while(str[endIndex] <= '9' && str[endIndex] >= '0')
++endIndex;
if(endIndex-startIndex > 3 || endIndex-startIndex == 0)
return false;
if(str[endIndex] == '.'|| subNum == 4)
{
++subNum;
char* tempStr = new char[endIndex-startIndex+1];
for(int i=0; i< endIndex-startIndex; ++i)
tempStr[i] = str[startIndex+i];
tempStr[endIndex-startIndex] = '\0';
bool isValid = isValidIpSubInt(tempStr);
delete[] tempStr;
if( isValid == false)
{
return false;
}
}
else{
break;
}
startIndex = ++endIndex;
}
if( subNum == 5 || startIndex > length)
{
return true;
}else{
return false;
}

}
//1-3位字符串转换为255以内的整数,如果转换不成功则返回false
//具体判断方法,如果转换后的整数不在0-255之间的,则返回false
//001此类的认为是非法的,所以需要判断转换后整数的位数和字符串的长度是否一致,如果不一致则返回false
bool isValidIpSubInt(char* str)
{
if(str == NULL || strlen(str) == 0 )
return false;
int length = strlen(str);
int result = 0;
while(*str != '\0')
{
if(*str < '0' || *str >'9')
{
return false;
}
result = result*10 + *str-'0';
++str;
}
if(result <= 255 && result >= 0 && *str == '\0')
{
int resultLength = 1;
int tempResult = result;
while( tempResult /=10 )
++resultLength;
if(resultLength != length)
return false;
}else{
return false;
}
return true;
}
void Test(char* testName,char* testString,bool expectedResult)
{
cout << "test: " << testName << endl;
cout << "String is :" ;
if(testString == NULL)
{
cout << "NULL" << endl;
}else
{
cout << testString << endl;
}
bool result = isValidIpAddress(testString);
//cout << " the result is :" << result << endl;
//cout << "expected result is :" << expectedResult << endl;
cout << testName << " ";
if( result == expectedResult)
{
cout << "passed !" << endl;
}
else
{
cout << "failed !" << endl;
}
}
//测试空串
void Test1()
{
char* str = NULL;
Test("Test1",str,false);
}
//测试超过256的串
void Test2()
{
char* str = "255.255.256.255";
Test("Test2",str,false);
}
//测试没有有效输入的串
void Test3()
{
char* str = "1.B.C.D";
Test("Tes3",str,false);
}
//测试前面是0的串
void Test4()
{
char* str = "01.0.0.0";
Test("Test4",str,false);
}
//测试正确最长串
void Test5()
{
char* str = "255.255.255.255";
Test("Test4",str,true);
}
//测试正确最短串
void Test6()
{
char* str = "0.0.0.0";
Test("Test6",str,true);
}
int main()
{
Test1();
Test2();
Test3();
Test4();
Test5();
Test6();
system("pause");
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐