合法IP

来源:互联网 发布:腾讯管家域名检测 编辑:程序博客网 时间:2024/06/10 21:08
#include<iostream>#include<string>#include<stdlib.h>using namespace std;int main(){    string input;    cin>>input;    int k = 0;    int location[4] = {0};    int flag = 1;    for(int i = 0; i < input.length(); i++){        if(input[i] == '.') location[k++] = i;    }    if(k != 3){        cout<<"NO"<<endl;        return 0;    }    int tmposition = 0;    for(int i = 0; i < 3; i++){        int position = location[i];        string str = input.substr(tmposition, position - tmposition);        int intIP = atoi(str.c_str());        if( intIP < 0 || intIP > 255){            cout<<"NO"<<endl;            return 0;        }        tmposition = position + 1;    }    string str = input.substr(location[2]);    int intIP = atoi(str.c_str());        if( intIP < 0 || intIP > 255){            cout<<"NO"<<endl;            return 0;        }    cout<<"YES"<<endl;    return 0;}

0 0