File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String Revers

 
vigneswar rao
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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é
Sheriff
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
vigneswar rao
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
j--;
}
System.out.print(ch);
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by vigneswar rao:
...System.out.print(ch);

If you correct this to print the array elements (by iterating through), then you will see how your approach is working.
 
Nikhil Menon
Ranch Hand
Posts: 70
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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é
Sheriff
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about using StringBuffer?
StringBuffer sb = new StringBuffer(s);
sb.reverse();

OR

char[] str = new char[s.length()];
s.getChars(0,s.length(),str,0);
loop through this array and reverse it.
 
Tony Morris
Ranch Hand
Posts: 1608
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
http://contractualj.com/coverage-report/_files/1f.html
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic