File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

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.

Steve Luke

Joined: Jan 28, 2003
Posts: 4181

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.

Rob Spoor

Joined: Oct 27, 2005
Posts: 20271

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).

How To Ask Questions How To Answer Questions
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 684
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.

I agree. Here's the link:
subject: Reverse String with using String class methods?
It's not a secret anymore!