5 ms·
The program also seems to work if, in the loop header, you replace “i <= strlen(z_terminated)” with “z_terminated[j] != 0”. I don’t understand why I have to pu
by sethg 16y ago
The program also seems to work if, in the loop header, you replace “i <= strlen(z_terminated)” with “z_terminated[j] != 0”.
I don’t understand why I have to put j in the index there, rather than j-1: after the string-ending null is copied, j is incremented, right?
- studer 16y agoWell, if you don't understand your own code, it's probably not a good idea to write it that way (for-loops are a lot easier to read & verify if the stop condition depends on the loop variable, for example). But your solution isn't just hard to read, it's also horribly broken. If you cannot figure out why, try printing the value of 'i' inside the loop.
- nkurz 16y agoI don't think 'j' nor 'j-1' would make sense here. 'i' would make sense, if you also terminate the condensed string. Maybe it would be clearer if the names of the index variables better described their purpose? http://gist.github.com/416298 http://gist.github.com/416298 (no peaking if you're submitting!) I think these names make it clearer that neither "z_terminated[condensed]" nor "z_terminated[condensed - 1]" are reliable ways to find the end of the original string.