Determine the validity of an IP address

Source: Internet
Author: User
/************************************************************************* 
	> 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;

   Has a leading 0, illegal//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) {//Please implement if (Pszipaddr==null) return false here;
    String str=string (PSZIPADDR);
	cout<<str<<endl;

	int Len=str.length ();   int coma_num;  =numberofcoma (str);

	Count number, if the number of dots is not equal to 3 is illegal//at the end of STR plus a decimal point, in order to statistically convenient str.append ("."); The following starts the split string, split with the point number, get each string, if the string, illegal, then return false, illegal cases include a space in the middle of the string, the string has a leading 0, the number in the string is no longer between 0-255, and the string is empty, these are illegal//
    
    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))//If not valid, jumps out and returns 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; }

--------------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------------------- ------

The Find and SUBSTR functions of C + + are used:

int find (char c, int pos = 0) const;//Look for the character C at the position of the current string int found (const char *s, int pos = 0) const;//Look up the string s in the current string from POS Position int Find (const char *s, int pos, int n) const;//look up the position of the first n characters in the string s in the current string by starting with the POS int found (const string &s, int pos = 0) C onst;//from Pos to find the position of string s in the current string//Find the location of success, return the value of String::npos int rfind (char c, int pos = NPOs) const;//
Look up the character C in the current string from the point of POS 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; From the beginning of the POS to find the position of the first n characters in the string s in the current string, successfully return to the location, and return string::npos value int find_first_of (char c, int pos = 0) const;//
Starting from Pos Find the first occurrence of the character c the position 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; Starts from the POS to find the position of the first character in the current string in an array of the top n characters of S.
Lookup failure Returns 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; Intfind_first_not_of (const char *s, int pos,int n) const;
int find_first_not_of (const string &s,int pos = 0) const;
Finds the first occurrence of a character not in string s from the current string, and the failure returns 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 and find_last_not_of are similar to find_first_of and find_first_not_of, just looking forward from the back

These are the functions in the string class, in addition, in the C + + STL also provides the Find function on the use of each container


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.