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:strtok
function (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;
}
};