AlgorithmCrackers / Interview-Questions

List of all the Interview questions practiced from online resources and books
246 stars 127 forks source link

check StringReverseInplace #2

Closed prakashn27 closed 9 years ago

prakashn27 commented 9 years ago

I am getting runtime exception while running the code. but it looks correct to me. Check and let me know where i am wrong.

prakashn27 commented 9 years ago

See this first example in c dude. it is so awesome :) http://stackoverflow.com/questions/198199/how-do-you-reverse-a-string-in-place-in-c-or-c

santhoshvai commented 9 years ago

The issue is that,

if the length of string is 5 then char[0..4] .. so you should send 4 instead of 5 to fix the bug.. 

See this code.. http://ideone.com/Rvp7aY change your implementation accordingly

prakashn27 commented 9 years ago

no worries dude. i figured it out. return new String(name); this works :+1:

santhoshvai commented 9 years ago

So the lesson is to test the .toString method always and always add it to our custom dataStructures.. because it

Returns a string representation of the object.
 In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read. 
It is recommended that all subclasses override this method. 

http://stackoverflow.com/questions/3615721/how-to-use-the-tostring-method-in-java