IP 点分四段


#include 
#include 

using namespace std;

char * my_strtok(char *p, const char *str)
{
    static char *temp;
    int i=0;
    static int bit;

    if(p!=NULL)
        temp = p;
    else
        temp += bit+1;

    while(*(temp+i)!=*str && *(temp+i)!='\0')
    {
        i++;
    }

    if(*temp=='\0')
    {
        temp = NULL;
    }

    bit = i;

    if(temp!=NULL)
        *(temp+i) = 0;

    return temp; 
}


int main()
{
    char str[30] = {0};
    char ptr[30] = {0};

    cout << "input str: " ;
    cin >> str;
    cout << "input ptr: ";
    cin >> ptr;

    cout << "str: " << str << endl;
    cout << "ptr: " << ptr << endl;


    char *p = strtok(str, ".");
    cout << "first p: " << p << endl;

    while((p=(strtok(NULL, "."))) != NULL)
    {
        cout << p << endl;
    }

    char *q = my_strtok(ptr, ".");
    cout << "first q: " << q << endl;

    while((q=(my_strtok(NULL, "."))) != NULL)
    {
        cout << q << endl;
    }

    return 0;
}

你可能感兴趣的:(IP 点分四段)