• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

String Revers

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic