jQuery in Action, 3rd edition
The moose likes Beginning Java and the fly likes String Revers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String Revers" Watch "String Revers" New topic

String Revers

vigneswar rao

Joined: Feb 12, 2006
Posts: 14
hi can we write a String Reverse program without using any built in methods() . ie, String.length(),charAt(),..........etc
if possible that i want code.
Christophe Verré

Joined: Nov 24, 2005
Posts: 14688

If you are using a character array as input, you can easily do this.
If you are using the String class and still don't want to call things like charAt or toCharArray or others, this does not seem possible.

As this sounds like an assignment, I won't post code
Show us what you've got first.

[My Blog]
All roads lead to JavaRanch
vigneswar rao

Joined: Feb 12, 2006
Posts: 14
I'm doing like this ...

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the String:");
String s=br.readLine();
char ch[]=new char[20];
int k= s.length();
int j=k;
for(int i=0; i<s.length(); i++)
ch[j] = s.charAt(i);
marc weber

Joined: Aug 31, 2004
Posts: 11343

Originally posted by vigneswar rao:

If you correct this to print the array elements (by iterating through), then you will see how your approach is working.

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
Nikhil Menon
Ranch Hand

Joined: Nov 22, 2004
Posts: 70
Try the code that I have written below.

You can use the RandomAccessFile to temporarily write your string into a file and the while reading from the same file you can read it in the reverse direction.
RandomAccessFile gives you the luxury of accessing the file pointer (cursor) so you can define where to read in the file.

PS: By the way why do you want to avoid the built in String and StringBuffer methods?
Christophe Verré

Joined: Nov 24, 2005
Posts: 14688

Here are my tips:
1. I would set the size of the array not to 20, but the the length of the input string
2. I would read from the end of the string and go down to the head
3. The variable k is useless
Rahul Gujarathi

Joined: Oct 23, 2002
Posts: 10
How about using StringBuffer?
StringBuffer sb = new StringBuffer(s);


char[] str = new char[s.length()];
loop through this array and reverse it.

Rahul G.<br />SCJP 1.4<br />-----------------<br />Be Good, Do Good!
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
All of the suggested solutions so far have the disadvantage that a data copy must occur. This is the result of failure of abstraction on behalf of java.lang.String - specifically, failure to declare all public methods to an interface. If you assume the interface of java.lang.CharSequence, you can achieve a reverse without ever copying the String data.

Want a hint at implementation?
The source code to net.tmorris.adt.sequence.ReverseSequenceImpl gives some clues.

Tony Morris
Java Q&A (FAQ, Trivia)
I agree. Here's the link: http://aspose.com/file-tools
subject: String Revers
It's not a secret anymore!