| Author |
Regarding String Object Reversing
|
surya.raaj prakash
Ranch Hand
Joined: Oct 30, 2009
Posts: 76
|
|
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
Joined: Oct 13, 2005
Posts: 32708
|
|
|
You tell us what you have thought of so far. It is quite an easy task.
|
 |
gk karthayani
Greenhorn
Joined: Nov 13, 2009
Posts: 2
|
|
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()
|
regards
karthayani k
|
 |
Somnath Mallick
Ranch Hand
Joined: Mar 04, 2009
Posts: 471
|
|
|
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
Joined: Aug 05, 2005
Posts: 3011
|
|
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.
|
Joanne
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32708
|
|
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
Joined: Nov 13, 2009
Posts: 2
|
|
i get the output "olleH dlroW" from the following code.
is this wrong?
please clarify...
|
 |
Embla Tingeling
Ranch Hand
Joined: Oct 22, 2009
Posts: 237
|
|
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
Joined: Aug 12, 2008
Posts: 140
|
|
Hi,
you can easily done this with using org.apache.commons API
|
 |
Embla Tingeling
Ranch Hand
Joined: Oct 22, 2009
Posts: 237
|
|
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?
|
 |
 |
|
|
subject: Regarding String Object Reversing
|
|
|