+ 2

how to remove useless output after reverse of string in code?

https://code.sololearn.com/cwTpUaphjD4l/?ref=app

18th Oct 2018, 1:18 PM
Rishab Sharma
Rishab Sharma - avatar
7 Réponses
+ 3
You have: for(i=l;l>=0;i--) That should be: for(i=l;i>=0;i--) Your original code's for loop basically never stops, so it ends up reading garbage in memory, since i keeps decrementing indefinitely. All the junky characters you see is your program interpreting as characters the contents of the memory following the array b.
18th Oct 2018, 1:27 PM
fra
fra - avatar
+ 3
thnks
18th Oct 2018, 1:31 PM
Rishab Sharma
Rishab Sharma - avatar
+ 2
btw any suggestion how to remove silly mistkaes like this? any suggestions for reading code?
18th Oct 2018, 1:32 PM
Rishab Sharma
Rishab Sharma - avatar
+ 1
I correct myself for the last part: the junky memory is the one preceding b, since i becomes a negative number
18th Oct 2018, 1:31 PM
fra
fra - avatar
+ 1
Well, you need to get used to read code, and that comes with experience. Such mistakes can be hard to find. A big warning sign in this case was the junky characters: that is usually a sign of reading garbage and interpreting it as a string, something that happens when you forget the null terminator. There are some tools that can help though: on Linux there is Valgrind, that detects this kind of mistakes (and much more).
18th Oct 2018, 6:59 PM
fra
fra - avatar
+ 1
oh thnks a lot for this info
18th Oct 2018, 7:05 PM
Rishab Sharma
Rishab Sharma - avatar
+ 1
You're very welcome! :)
18th Oct 2018, 7:11 PM
fra
fra - avatar