It looks to me like that weird for loop is running you past the end of the string you get and into the next.
The environment variables are probably held one after another in memory, like this "value0another0this0" with the 0 terminator between them. The for loop is trying to find an "=" sign in the environment variable, probably because the original code expects something like "chars=aeiou." But when it doesn't find one it just pushes past the end, and you end up reading the next environment variable when you print the string.
Just a guess, I don't have time to test that theory right now.
Jay