Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regarding String Object Reversing

 
surya.raaj prakash
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Friends,

Can Anybody explain,How can you reverse the string object?

But I want the reverse order like this "olleH dlroW"
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You tell us what you have thought of so far. It is quite an easy task.
 
gk karthayani
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The efficient way to reverse a given string is use reverse() method of java StringBuffer class.

reverse() method returns the StringBuffer object so we need to cast it back to String using toString() method

we can also reverse a given string by recursive method using subString()

 
Somnath Mallick
Ranch Hand
Posts: 483
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please put in some effort to go through the Java API before posting. Or Google! Things learned by yourself stay on your mind a lot longer than just told!
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
gk karthayani wrote:The efficient way to reverse a given string is use reverse() method of java StringBuffer class.


If you look at the example that the OP gave you will see that reverse will not do what is required.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, thank you, Joanne. I hadn't noticed what he had written.

Don't use the reverse() method because you won't get full marks. What you have posted is not the String reversed. It is the String with each of its words reversed. That is only slightly more difficult than a full reversal.

And welcome to JavaRanch gk karthayani
 
gk karthayani
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i get the output "olleH dlroW" from the following code.





is this wrong?
please clarify...
 
Embla Tingeling
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
gk karthayani wrote:is this wrong?
please clarify...


No it's quite clever actually. You reverse the whole string and then split it into separate words. The conventional approach probably would be to first split the string into words and then reverse each word separately. The latter involves more work so your solution has a slight speed advantage.

The problem is that the OP most likely is supposed to come up with a more low-level algorithmic approach. It's important during education otherwise the new generation Java programmers will become "API suckers" who are totally lost when they can't find a Java library method that exactly fits the bill.
 
Raj chiru
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

you can easily done this with using org.apache.commons API

 
Embla Tingeling
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
raj chiru wrote:
you can easily done this with using org.apache.commons API


Fine, everybody knows Java programming has become The Noble Art of Method Searching.

The question is, can Java programmer's today do anything without the help of a method from the shelf? I have serious doubts. I saw this question at a forum recently: - I have this positive int. What method should I use to make it negative?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic