The problem here is the condition in the for-loop. That condition is fully evaluated after every iteration. That means not just the r<
part, but also the strlen(number)
part. This is generally a bad idea – strlen
must check the whole sting to find the first \0
. In very exceptional situations that might make sense, but here it does not. You want to use the original, unmodified string length.
If you understand strings a bit better, you might realize that you don’t need r
at all. You can simply set characters to \0
until you find one that’s already \0
.