Properly allocate memory and terminate strings in strtok_r