1. String Manipulation / Juxtaposition

Would someone be kind enough to help me with this seemingly simple (yet difficult for a non-C guy like me) problem...

Assuming buf contains a string similar to either one of these:

Code:
```50005112601012121148\n in buf
... or ...
600000112401092112156\n in buf```
How do I juxtapose/manipulate the numbers so that I end up with the following:

Code:
```010.112.148.212 in ip, and 5000 in port.
... or ...
010.112.156.211 in ip, and 60000 in port.```
The logical formula for the decode is as follows (from right to left):
- last three bytes of string are the third IP octet.
- prior byte is junk, and can be anything. Ignore.
- prior three bytes of string are the fourth IP octet.
- prior byte is junk, and can be anything. Ignore.
- prior three bytes of string are the first IP octet.
- prior byte is junk, and can be anything. Ignore.
- prior three bytes of string are the second IP octet.
- prior byte is junk, and can be anything. Ignore.
- prior bytes (might be 4 or 5 in length) are the port.

I can do it in a relatively straightforward way, assuming the string is always the same length. But I don't know an easy way of doing this with a string that might vary in size. Here's how I'm doing it, but of course it doesn't work for the shorter (first example) string...
Code:
```    f = buf;
*(f + 5) = '\0';

servers[x].port = atoi(f);
f += 6;

*(f + 3) = '\0';
*(f + 7) = '\0';
*(f + 11) = '\0';

if (*(f + 4) == '0')
strcpy(servers[x].ip, (f + 5));
else
strcpy(servers[x].ip, (f + 4));

strcat(servers[x].ip, ".");
strcat(servers[x].ip, (f + 0));
strcat(servers[x].ip, ".");
strcat(servers[x].ip, (f + 12));
strcat(servers[x].ip, ".");
strcat(servers[x].ip, (f + 8));```
Help (in the form of some code) would be greatly appreciated at this time... my mind is melting.

2. It appears that the string has to be 20 or 21 bytes, not counting \n? (And I assume you mean \0, the string termination character.) In that case, use strlen to find out which it is -- that will tell you whether you need to use four or five for the beginning read; and the rest is gravy.

3. Thank you, tabstop... I just combined three lines into an if/else block and that took care of the beginning stuff (the port number) without having to re-engineer the rest of the code.