I'm looking to retrieve bits of information from a char string.
Basically I get a string with delimiters in a few spots, then I have to get the substrings between each of those delimiters.
For instance, I have a char string that gets an IP address such as "150.40.230.8". And what I have to do is extract the "150", "40", "230", and "8" in individual elements of a [4] element array of type int.
And I want to get those into an array called octet[4]. So in the end I want-Code:char ip_address[16] = ""; cout << "Enter an IP: "; cin >> ip_address;
octet[0] = 150
octet[1] = 40
octet[2] = 230
octet[3] = 8
I was thinking of using the strtok() function, but wasn't sure exactly how to get it to work. Or perhaps the strchr() function in some way?
Maybe somehow I could get the position of the first ".", read the stuff before that into octet[0], then from that position find the position of the next "." and read in the stuff between those two positions into octet[1], and so on until I read the last octet where I read from the position of the last "." to the null terminator. Just not sure how the code would look for that.
Any ideas?