Determines whether the input string is an IP address.

Source: Internet
Author: User

First, the prototype of a c function is given: int sscanf (const char * buffer, const char * format, [argument]...) its return value is the parameter data, that is, the number of argument, buffer: the stored data, format: format control string, argument: select the string. This program reads data from the standard stream and can carry out unlimited input. The following code is pasted, which leads to another problem. Convert the string ip address to an integer ip address.
[Cpp]
# Include <stdio. h>
# Include <string. h>
Int main (void)
{
Char str [32];
Int a, B, c, d;
Int ret = 0;
While (fgets (str, sizeof (str), stdin )! = NULL)
{
Int len = strlen (str );
Str [len] = '\ 0 ';
Ret = sscanf (str, "% d. % d", & a, & B, & c, & d );
If (ret = 4 & (a> = 0 & a <= 255) & (B> = 0 & B <= 255) & (c> = 0 & c <= 255) & (d> = 0 & d <= 255 ))
{
Printf ("it is ip! \ N ");
}
Else
Printf ("it is not ip! \ N ");
}
Return 0;
}

Gcc-Wall ip. c-o ip
12.3.4.5
It is a ip address!
The following introduces another problem. In many cases, the string ip address must be converted to an integer ip address. You can also apply the sscanf function to store the four fields to a, B, c and d are removed from the four variables before the shift operation. Because the IP address is a 32-bit and unsigned integer variable, the unsigned int can be used for storage. unsinged int ip = (a <24) + (B <16) + (c <8) + d.

Related Article

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.