• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Regarding String Object Reversing

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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"
 
Marshal
Posts: 80624
470
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You tell us what you have thought of so far. It is quite an easy task.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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()

 
Ranch Hand
Posts: 483
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 80624
470
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i get the output "olleH dlroW" from the following code.





is this wrong?
please clarify...
 
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic