• Post Reply Bookmark Topic Watch Topic
  • New Topic

String reversing without using String functions...

 
Chandra Bairi
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello friends,
Can anyone help me out in reversing the string without using the String functions provided by java API.No method for string should be used.
 
Mark Vedder
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at the StringBuffer class in the Java API. Even saying that might be a bit too much information for what sounds like a homework problem.
 
Chandra Bairi
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Mark,
I have done that using StringBuffer. But is there anyway where you dont use String and StringBuffer (they are almost same...not completely but atleast in some issues) so without using the API for both String and StringBuffer can we do that.
Is is possible.
Thanks in any case.
shekar.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. For a reasonable solution, you'd make use of at lease a couple of the methods available to String objects. charAt and length might compose a minimum set needed to avoid throwing an exception and to create a working solution.
To be honest, I can imagine one strategy of using a regex, another using I/O that would each avoid all String methods, and another using something like a web service, but I'd not venture down those roads unless following an assignment and being certain that the rules forbid all String methods.
I'm almost certain I once had an instructor a while back ask a very similar question with the "no API" requirement. When he started on his solution, he immediately started using something like the length method and then proclaimed that the students had misunderstood his requirements. Chandra, does this exercise come from a book or a web site?
[ March 01, 2004: Message edited by: Dirk Schreckmann ]
 
Chandra Bairi
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello dirk
thanks for the solution. can you tell me the initial solution using the regular expressions and the i/o so that I can have a look at it. I need to know them. and i did not get it from any book. i was asked to do this.please if you could send me the solutions. thanks a lot once again.
regards
shekar.
 
sever oon
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have to agree with Dirk--not using ANY functions of the String class seems like a fool's errand. What could you possibly glean from performing such a feat that would be useful knowledge about Java or programming in general?
It seems to me that you'd at least need to get the array of characters in the String using String.toCharArray():

The only way i can see to completely avoid calling any method on the String class at all would be to replace the first two lines of code in the previous example with:

But here you're calling methods on StringBuffer, which you also said was not allowed.
Other than that, if you really are not allowed to use any methods at all on the String or StringBuffer class, I would say your only option is to use the Java Native Interface (JNI). This allows you to write a method in another language like C and call that method from java. This native method would take a String and return the reversed String, it would be written in a platform-dependent language like C, and you'd simply call it and have the result returned to you.
sev
 
Igor Ko
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Opps, the solution is already posted
But really I see here one problem ...
The problem is that now one java "char" isn't one UNICODE character,
exist some characters that represented as two java 'char'
Previously java char was UNICODE character, now it's UTF-16 representation
of 22 bits (?) UNICODE character.
(See also:
documentation java 1.5 java.lang.Character surrogate char.)
and http://www.tbray.org/ongoing/When/200x/2003/04/26/UTF
So if string was char[3] = { A B1 B2 }
after reversing: B2 B1 A ,that is wrong.
Where B UNICODE character is represented as two 16 bits java char: B1, B2
[ March 01, 2004: Message edited by: Igor Ko ]
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Chandra Bairi:
please if you could send me the solutions.

That's not how we like to do things around these parts.
If you were to post examples of what you're working on, I'm sure folks around here would be happy to help nudge you in the right direction.
 
Chandra Bairi
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Come on dirk,
i did not even know the solution. it was you who proposed the solution and i was asking how we could do that because you said that it is possible somehow by using regular expressions and another using I/O, so I asked you to send me the solutions. I never has a solution for that and whatever solutions sever and igor ko sent i have already done that. I am pretty aware of the fact that we can nudge a person who is fighting hard with a solution. but can you ask a person to come out of water who doesn't know how to swim. in any case if you would not like to send the solutions atleast hint me as to how to do them.
regards,
shekar.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the person in question wanted to learn to swim, I'd certainly suggest he get in the water.
Get your feet wet. We'll help to point out how to do the various strokes to stay afloat and get where you're going.
 
Chandra Bairi
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks dirk,
i certainly would like to swim. but using I/O how do I start that is the big problem. i have no idea about regular expressions and i cannot explore them right now but as far as I/o is concerned i would surely like to have a go but how do i start how can i do it?
Is it something like this. I need to put the string in file and read single character from the file for string and then store them in another stringbuffer and then print it.
is that right?
regards
shekar.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you've time, regular expressions are a powerful mechanism to do quite a few useful things with Strings and you'd likely benefit knowing how to use them. You could start such learning with the "An Introduction to java.util.regex - Part 1" and "An Introduction to java.util.regex - Part 2" articles I wrote for the JavaRanch Journal. I'd also recommend Max's latest book, "Java Regular Expressions: Taming the java.util.regex Engine".
As far as a potential I/O solution is concerned, you don't need to involve a file. You could send the String contents out through one type of stream, then read them in as a byte array (or perhaps something is available to read the data in as a character array) with another type of stream. Then, manipulate the array to effectively reverse the order of the original String contents. And finally convert the array into a String. This last step could be done with a String constructor (if converting from a byte array), or you could even avoid that by sending the array out and in another set of streams, this time reading the data in as a String.
For a decent introduction to learning I/O in Java, I'd suggest reading Sun's Java Tutorial on the subject. (Note that Sun's Java site has been down for quite a few hours now. Try them later. I'm sure they'll be back up.)
Moving this to the I/O forum...
[ March 02, 2004: Message edited by: Dirk Schreckmann ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!