aspose file tools*
The moose likes Beginning Java and the fly likes Reverse String with using String class methods? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Reverse String with using String class methods?" Watch "Reverse String with using String class methods?" New topic
Author

Reverse String with using String class methods?

Skanda Raman
Ranch Hand

Joined: Mar 21, 2008
Posts: 205


I have a String called "Hello World". I want to reverse the same with out using any method in String Class.

Please let me know how can i do this.


Skanda
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

This sounds like a homework question.

Pages that may help you:
Do your own homework.
We aren't a CodeMill
Showing Some Effort Helps a lot


That said, assuming that you actually have a java.lang.String, then it is impossible to reverse the String without using at least 1 String method. Or at least, one method from some other core Java class (like StringBuffer, or StringReader, etc...) which would call String methods.


Steve
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19785
    
  20

You can cheat using the StringBuilder class, but that's not what you want

You can't do this without calling any method of String. That's because it's internals are not accessible without methods. Therefore, there is a minimum of one method to call to make those internals accessible. That method is toCharArray(). You may also want to check out a combination of length() and charAt(int) - those allow you to do the same but without creating a new char[] (which toCharArray() does).


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
This sounds like an introductory exercise in recursive programming that I once had. By using length() and charAt(int) as Rob suggested, you can sort of simulate a situation where you are getting characters one at a time. I think this is still in the spirit of the question. Just make sure you read the string from the beginning, and don't use an array or another string to store the characters in reverse.

The following link may be useful for you.

http://erwnerve.tripod.com/prog/recursion/recintro.htm

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reverse String with using String class methods?