C ++ determines IP Address

Source: Internet
Author: User

Save it temporarily and test it later

// Determine whether an IP address is valid for 2 h <br/> void main (void) <br/>{< br/> char s [16]; <br/> memset (S, '/0', 16); <br/> int IP Address [5] = {0, 0, 0 }; <br/> int I = 0, j = 0, Count = 0; <br/> cout <"Please input an IP address/N "; <br/> CIN> S; <br/> int strlenth = strlen (s ); // 15 </P> <p> // cout <strlenth <Endl; </P> <p> If (strlenth> 15) <br/> {// The maximum length is 15 <br/> cout <"Invalid Address/N"; <br/> exit (0 ); <br/>}</P> <p> else <br/> {<br/> for (I = 0; I <s Trlenth; I ++) <br/>{< br/> If (S [I]> '0' & S [I] <'9 ') | (s [I] = '. '))&&! Strstr (S ,".. ") // All are numbers or dots, and there are no Links <br/>{< br/> // convert to a value <br/> If (s [I] = '. ') <br/>{< br/> count ++; <br/> J ++; <br/>}< br/> else <br/> IP [J] = IP [J] * 10 + (s [I]-'0 '); <br/>}< br/> else <br/>{< br/> cout <"Invalid Address/N"; <br/> exit (0 ); <br/>}< br/> If (count> 3) <br/> {// too many segments <br/> cout <"Invalid Address/N"; <br/> exit (0 ); <br/>}< br/> // cout <IP [0] <'. '<IP [1] <'. '<IP [2] <'. '<IP [3] <Endl; </P> <p> for (j = 0; j <4; j ++) <br/> {// determine the value range <br/> If (IP [J]> 255) <br/>{ <br/> cout <"Invalid Address/N"; <br/> exit (0 ); <br/>}< br/> cout <"valid address/N"; <br/>}< br/>}

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.