Your first variant is unnecessary detailed and hard to read:
an IPv4 address consists of exactly 4 digit groups
4 is a small number, maybe it's worth to inline the for loop
the for loop treats the case k == 0 specially
the += operator for strings allocates a new string each time
the code is much longer than the description in the task "join the four numbers ...