LeetCode algorithm solution to a problem of the 1108-IP address is not valid

Title Description

answer:

Speaks to address each string .separated into an array of strings and then, after the last addition to finally join a string [.]on it.
The separation is used: strtokfunction (char string will be represented)
char
-> String: Direct cast
string -> char *: char * str1 = const_cast <char *> (str2.c_str ());

Code:

class Solution {
public:
    string defangIPaddr(string address) {
        char* address_Char = const_cast<char *>(address.c_str());
        vector <string> vec;
        char* result = NULL;
        result = strtok(address_Char,".");
        while(result != NULL)
        {
            vec.push_back((string)result);
            result = strtok(NULL,".");
        }
        string res = "";
        for(int i = 0; i < (int)vec.size(); i++)
        {
            res += vec[i];
            if(i != (int)vec.size()-1)
                res += "[.]";
        }
        return res;
    }
};
He published 197 original articles · won praise 18 · views 20000 +

Guess you like

Origin blog.csdn.net/qq_41708792/article/details/104619045