Huawei OJ Test question--determine if the input string is a valid IP address (with the most complete test data in the world)

Source: Internet
Author: User

Topic title:

    • Determines whether the input string is a valid IP address

Detailed Description:

Please implement the following interface

< Span style= "margin:0px; padding:0px; Font-family: new song body; Color:blue; font-size:12px ">booli sipaddressvalid (const char * pszipaddr "

Input: pszipaddr string

Output: True valid IP address, false, invalid IP address

Constraints

  1. Input IP is XXX.XXX.XXX.XXX format

  2. The string ends with a space that is considered a legitimate IP

  3. The middle of the string contains spaces that are considered illegal IPs

  4. Similar to 01.1.1.1, the 1.02.3.4 IP sub-segment starts with 0 as an illegal IP

  5. A single 0 is considered legitimate ip,0.0.0.0 is also counted as a legitimate IP


This topic is very simple, but our code can not be tested, for what, because this format requires a lot of default, we have to do careful people!

Give multiple sets of test data:

/**********************************

Null

0.0.0.0

01.2.0.6

255.2.256.0

256.22.234

34.45.345.6

2.3.3.3

0.1.0.0

00.00.32.67

**********************************/

Here is the code, Welcome to add!

#include "IPAddressValid.h" #include <iostream>using namespace std; #include "IPAddressValid.h"  #include <cstring>  #include <iostream>  #include <cstdio>  using namespace std;    BOOL Isipaddressvalid (const char* pszipaddr) {if (Pszipaddr==null | | pszipaddr== ") return false;    Please implement int Slen=strlen (PSZIPADDR) here;//cout<<slen<<endl;int I,j,k,m,n,sum;int Sublen[10];const Char *ss= Pszipaddr;<span style= "White-space:pre" ></span>
<span style= "White-space:pre" ></span>//remove the Kinsoku space for (I=0;i<slen && ss[i]== "; i++); if (I==slen) Return false;for (j=slen-1;j>=0 && ss[j]== "; j--); if (j==0) return false;if (I>=J) return false;
<span style= "White-space:pre" ></span>//determine if there are spaces in the middle of the string, and if so FalseFor (k=i;k<j;k++) if (ss[k]== ") return false;//cout<<i<< "" <<j<<endl;
<span style= "White-space:pre" ></span>//record string appears '. ' If the point appears in the first character or adjacent element is also point falsem=0;for (k=i;k<=j;k + +) {if (ss[k]== '. ') {sublen[m++]=k;if (k==i | | k==j) return false;if (ss[k+1]== '. ') return false;}}
<span style= "White-space:pre" ></span>//if the number of points is not 3 then Falseif (m!=3) return false;sublen[m++]=j+1;//for (k=1; k<m;k++)//if ((sublen[k]-sublen[k-1]) >4) return false;//for (k=0;k<m;k++)//cout<<sublen[k]<< " ";//cout<<endl;
<span style= "White-space:pre" ></span>//determine whether each sub-segment is 0, if it is the only 0 if the book is not, if not falsen=0;k=i;while (n<m) { For (K=i;k<sublen[n] && ss[k]== ' 0 '; k++)//if (ss[k]!= ' 0 ' && k<sublen[n] && k>i) Return false;if (ss[k]== ' 0 ') {if (K+1<slen && ss[k+1]!= '. ') return false;} k=sublen[n]+1;n++;}
<span style= "White-space:pre" ></span>//determine whether the integer conversion of each sub-paragraph is between 0~255, if not Falsen=0;while (n<m) {sum=0;for (k =i;k<sublen[n];k++) {if (ss[k]< ' 0 ' | | ss[k]> ' 9 ') return false;sum=sum*10+ss[k]-' 0 ';} if (sum>255) return false;i=sublen[n]+1;n++;}    return true;}



Huawei OJ Test question--determine if the input string is a valid IP address (with the most complete test data in the world)

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.