/*************************************************************************
> File Name: newfile1.cpp
> Author: pzz
> Mail: 837157806@qq.com
> Created Time: 2014/2/24 14:06:47
************************************************************************/
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<list>
#include<string>
#include<algorithm>
#include<iterator>
#include<ctype.h>
using namespace std;
inline bool isok(string str,int num)
{
int len=str.length();
int d=0;
//if(num!=0&&str[0]==' ') return false;
if(str=="0") return true;
if(len>1&&str[0]=='0') return false; //有前导0,不合法
//cout<<"str="<<str<<",num="<<num<<endl;
for(int i=0;i<len;i++)
{
// if(str[0]==' ') cout<<"kongge"<<endl;
if(!isdigit(str[i])&&str[i]!=' ') return false;
if(str[i]==' ')
{
if(num==2||num==3) return false;
else
{
//if(num==1&&str[0]==' ') return false;
//if(num==4&&str
if(num==1&&i!=0&&str[i]==' ') return false;
if(num==4&&i!=len-1&&str[i]==' ') return false;
}
}
//if(&&str[i]==' ')
if(isdigit(str[i])){
d=d*10+str[i]-'0';
}
// cout<<"d="<<d<<endl;
}
if(d<0||d>255) return false;
return true;
}
bool isIPAddressValid(const char* pszIPAddr)
{
// 请在此处实现
if(pszIPAddr==NULL) return false;
string str=string(pszIPAddr);
//cout<<str<<endl;
int len=str.length();
int coma_num; //=numberofComa(str); //计数点号,如果点号的个数不等于3个则为非法
//在str最后加上一个小数点,为了统计方便
str.append(".");
//下面开始分割字符串,用点号进行分割,得到每个字符串,如果字符串,不合法,则返回false,不合法情况包括,字符串中间有空格,字符串有前导0,字符串中数字不再0-255之间,还有字符串为空,,这些都是不合法
//string::iterator iter_pre=str.begin(),iter_post;
int iter_pre=0;
int iter_post=str.find('.',iter_pre);
// cout<<iter_post<<endl;
if(iter_post==str.npos) return false;
coma_num=0;
while(iter_post!=str.npos)
{
coma_num++;
string substr=str.substr(iter_pre,iter_post-iter_pre);
if(substr=="") return false;
cout<<coma_num<<","<<substr<<endl;
if(!isok(substr,coma_num)) //如果不合法,跳出,并返回false
{
return false;
}
iter_pre=iter_post+1;
//if(iter_pre<=len-1)
iter_post=str.find('.',iter_pre);
}
if(coma_num!=4) return false;
return true;
}
int main(void)
{
const char *psz="123";
if(isIPAddressValid(psz)) cout<<"ok"<<endl;
else
cout<<"no"<<endl;
system("pause");
return 0;
}
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
用了c++的find和substr函数:
int find(char c, int pos = 0) const;//从pos开始查找字符c在当前字符串的位置 int find(const char *s, int pos = 0) const;//从pos开始查找字符串s在当前串中的位置 int find(const char *s, int pos, int n) const;//从pos开始查找字符串s中前n个字符在当前串中的位置 int find(const string &s, int pos = 0) const;//从pos开始查找字符串s在当前串中的位置 //查找成功时返回所在位置,失败返回string::npos的值 int rfind(char c, int pos = npos) const;//从pos开始从后向前查找字符c在当前串中的位置 int rfind(const char *s, int pos = npos) const; int rfind(const char *s, int pos, int n = npos) const; int rfind(const string &s,int pos = npos) const; //从pos开始从后向前查找字符串s中前n个字符组成的字符串在当前串中的位置,成功返回所在位置,失败时返回string::npos的值 int find_first_of(char c, int pos = 0) const;//从pos开始查找字符c第一次出现的位置 int find_first_of(const char *s, int pos = 0) const; int find_first_of(const char *s, int pos, int n) const; int find_first_of(const string &s,int pos = 0) const; //从pos开始查找当前串中第一个在s的前n个字符组成的数组里的字符的位置。查找失败返回string::npos int find_first_not_of(char c, int pos = 0) const; int find_first_not_of(const char *s, int pos = 0) const; int find_first_not_of(const char *s, int pos,int n) const; int find_first_not_of(const string &s,int pos = 0) const; //从当前串中查找第一个不在串s中的字符出现的位置,失败返回string::npos int find_last_of(char c, int pos = npos) const; int find_last_of(const char *s, int pos = npos) const; int find_last_of(const char *s, int pos, int n = npos) const; int find_last_of(const string &s,int pos = npos) const; int find_last_not_of(char c, int pos = npos) const; int find_last_not_of(const char *s, int pos = npos) const; int find_last_not_of(const char *s, int pos, int n) const; int find_last_not_of(const string &s,int pos = npos) const; //find_last_of和find_last_not_of与find_first_of和find_first_not_of相似,只不过是从后向前查找
以上这些是string类当中的函数,另外,在c++的stl中还提供了find函数对各个容器的用法