Implementation of "IPv4 address detection" in written test questions

Source: Internet
Author: User

The power of netizens is strong. Yesterday I posted a post about an interview/PEN question. It provides many solutions for netizens. I have to admit that I wroteCodeIs so bad. Even so, it is also necessary to discuss with everyone, it is also necessary to write your own thoughts. Only in this way can we make progress and make changes to optimize our bad code. Then I sent a piece of bad code, hoping to learn more beautiful and excellent code from netizens.

Test Questions

Determines whether a string is an IPv4 address. If true is returned, otherwise false is returned. Library functions cannot be used. You can use C/C ++, C #, or Java.

E.g. C/C ++: bool checkipv4 (char * IP ){}

 

Solution

This is what I think,Separate each string according.For example, "192.168.203.70" is divided into four integers: 192,168,203 and 70.Then, determine whether the range of the four integer types is 0 to 0 ~ Between 255. I always think that this kind of consideration is inappropriate. you should choose a method like a regular expression. But not write...

At first I wrote it like this.

Check IPv4

# Include <stdio. h> Bool Checkipv4 ( Char * IP ){  Char * P = IP address;  Char * Q = IP address;  Char * C;  Int I = 0 , S, Count = 0 ;  If (* P = '  .  ' ) //  Handle Special Cases          Return   False  ;  While (* P! = '  \ 0  '  ){  If (* P ='  .  ' | * (P + 1 ) = '  \ 0  ' ) //  Cut the string according '.'  {Count ++; //  Calculate the number of cut strings              If (* (P + 1 ) = ' \ 0  ' ) //  Process the last string  {I ++ ; Q = P ;}  Else  Q = P- 1  ; S = 0  ;  For (Int J = 1 ; J <= I; j ++) //  Convert the string to an integer.  {  Int X = * q- '  0  ' ; //  The value of '0' minus the character is an integer.                  For ( Int K = 1 ; K <j; k ++) X * = 10  ; S + = X; q -- ;} Printf (  "  % D \ n  "  , S );  If (S < 0 | S> 255 ) //  Determine whether the cut string is within the IPv4 range                 Return   False  ; I = 0  ;}  Else  {I ++ ;} P ++ ;}  If (COUNT = 4  )  Return   True  ; Else          Return   False  ;}  Int Main ( Void  ){  Char IP [] = "  A. v.0.0  "  ;  If  (Checkipv4 (IP) printf (  "  This address is an IPv4 address \ n "  );  Else  Printf (  "  This address is not an IPv4 address \ n  "  );  Return   0  ;} 

 

But obviously there is a problem.

When "192.168.203.70" is checked, the result is OK.

However, when the object to be tested is not a number, for example, "a.168.203.70", it can still be verified and cannot be considered.

Therefore, I checked whether each integer is 0 ~ If it is not within the range of 9, false is returned.

 
 If(X <0| X>9)//Determines whether X is 0 to 0 ~ Between 9Return False;

Thank youZddDoes not take into account the type "1... 3" appear continuous points. Sure enough. I handle it like this:

This is because

S = 0  ;  For ( Int J = 1 ; J <= I; j ++) //  Convert the string to an integer.  {  Int X = * q- '  0  ' ; //  The value of '0' minus the character is an integer.                  If (X < 0 | X> 9 ) //  Determines whether X is 0 to 0 ~ Between 9                      Return  False  ;  For ( Int K = 1 ; K <j; k ++) //  The Nth digit multiplied by the nth digit-1 digit 10. X * = 10  ; S + = X; q -- ;} 

The for loop in it will not be executed, so it will not be processed like "1... 3" with a continuous point. The code I changed is as follows:

S = 0 ;  Int X =- 1  ;  For ( Int J = 1 ; J <= I; j ++) //  Convert the string to an integer.  {X = * Q- '  0  ' ; //  The value of '0' minus the character is an integer.                 If (X < 0 | X> 9 ) //  Determines whether X is 0 to 0 ~ Between 9                      Return   False  ;  For ( Int K = 1 ; K <j; k ++) //  The Nth digit multiplied by the nth digit-1 digit 10. X * = 10 ; S + = X; q -- ;}  If (X =- 1  )  Return   False ; //  Prevent continuous points in type "1... 3" 

Place x outside the for loop to check whether consecutive points exist.

The complete code is as follows:

 

# Include <stdio. h> Bool Checkipv4 ( Char * IP) {printf (  "  Check object: % s \ n  "  , Ip );  Char * P = IP; //  Traversal pointer      Char * Q = IP address; //  String pointer      Int I = 0 , S, Count = 0 ; //  I is the length of each string, S is the string converted to an integer, and count is the number of strings      If (* P = '  .  ' ) //  Handle Special Cases          Return   False  ;  While (* P! = '  \ 0  ' ) //  Traverse each character  {  If (* P = '  .  ' | * (P + 1 ) = '  \ 0  ' ) //  Splits the string according to '.', and the last string is identified by '\ 0'.  {Count ++; // Calculate the number of cut strings              If (* (P + 1 ) = '  \ 0  ' ) //  When processing the last string, '\ 0' is identified  {I ++ ; Q = P ;}  Else  Q = P- 1 ;//  '. Splitters'  S = 0  ;  Int X =- 1  ;  For ( Int J = 1 ; J <= I; j ++) //  Convert the string to an integer.  {X = * Q- ' 0  ' ; //  The value of '0' minus the character is an integer.                  If (X < 0 | X> 9 ) //  Determines whether X is 0 to 0 ~ Between 9                      Return   False  ;  For ( Int K = 1 ; K <j; k ++)//  The Nth digit multiplied by the nth digit-1 digit 10. X * = 10  ; S + = X; q -- ;}  If (X =- 1  )  Return   False ; //  Prevent continuous points in type "1... 3"  Printf ( "  % D \ n  "  , S );  If (S < 0 | S> 255 ) //  Determine whether the cut string is within the IPv4 range                  Return   False  ; I = 0  ;}  Else {I ++ ;} P ++ ;}  If (COUNT = 4 ) //  Check whether there are four strings          Return   True  ;  Else          Return   False  ;}  Int Main (Void  ){  Char IP [] = "  1. 2.3  "  ;  If  (Checkipv4 (IP) printf (  "  This address is an IPv4 address \ n  "  );  Else  Printf (  " This address is not an IPv4 address \ n  "  );  Return   0  ;} 

 

 

 

The explanation should be clear. At this time, as long as the cut characters are not 0 ~ Within the range of 9, none of them will pass.

 

I believe there are better solutions. Please join us ....

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.