Topic Description
Determining whether the IP address valid, N rows, in the input string, ABCD format, are each an integer, the output is determined as a valid IP
输入样例:
2
1.2.3.4
172.168.0.300
输出:
Yes
No
Code
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
int n;
cin>>n;
while(n--) {
char s[100];
cin>>s;
int flag=1;
char *part=strtok(s,".");
while(part) {
int num=stoi(part);
if(num<0||num>255) {
cout<<"No"<<endl;
flag=0;
break;
}
else
part=strtok(NULL,".");
}
if(flag)
cout<<"Yes"<<endl;
}
}
Additional knowledge
strtok
1. function prototype
char *strtok(char s[], const char *delim);
2. header file
string.h
3. Function Function
Resolving string is a set of strings. s is the string to be decomposed, the delim a delimiter character (if incoming string, each character string passed both delimiter). When the first call, s point to the string to break down, and again after the call should s set to NULL.
4. Return Value: returns a string pointer to the next after a split, if the split has been unable to return NULL
An array of pointers to strings and string
char *s="abc";
A pointer pointing to a string constant, this time constant stored in the area abc, s stack pointer to the string can only access can not be modified, may be varied pointers
char s[10]="abc";
Stored in the stack area you can be modified
If you read only two can be used, modified only by an array of characters