• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to find reverse of a String?  RSS feed

 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I want to reverse a String that is input

This is what I have tried so far



But its not working it is return same string that is Input

Where I am wrong?

Thanks
 
Jain Pratik
Greenhorn
Posts: 14
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of using replace method, create one more empty string and start adding the characters one by one to it from back .



Second and simplest approach is to use StringBuffer object. StringBuffer object contains direct method to reverse the string.

 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Describe in plain English what you are trying to do on lines 22-23. Frankly, I don't see why you have to do that.

Another thing you could do is to step away from the keyboard and figure out the procedure manually, with pen and paper. When you can describe how to do the process manually, it's usually fairly straightforward to translate that into code.
 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But in case of StringBuffer code is not showing on Console...

You can check
 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
now its working

thanks
 
Liutauras Vilda
Sheriff
Posts: 4927
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kishor,

Doesn't seem that you learnt something from it.
Solve this problem using charAt method, so you could understand better how it happens.
 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Liutauras Vilda:will try your solution to.

Thanks for suggestion
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!