Meaningless Drivel is fun!*
The moose likes Java in General and the fly likes String Reversing. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "String Reversing." Watch "String Reversing." New topic
Author

String Reversing.

Prasad Chelur
Greenhorn

Joined: Feb 12, 2002
Posts: 10
I want to reverse the string eithout using any temprory variables,String Buffer and arrays.
I know that we can use charAt() method for reversing a string but how can u assigen the char value to string.
Bcz for me the original string after any user defined method should contain exactly the revrese of the original one.
Please Suggest me soln.
Regrads
Prasad


prasadcts
Shivaji Marathe
Ranch Hand

Joined: Jan 11, 2002
Posts: 203
Why don't you post the code you have written so far ?
It will be much easier for some to help you if they have the code you have created already.
Steve Deadsea
Ranch Hand

Joined: Dec 03, 2001
Posts: 125
Java strings are immutable. They cannot be changed. You cannot reverse a string in place as you would be able to do with C String ore a Java StringBuffer.
Immutable strings are good for security and the performance of substring(), however, you give up the ability to alter them.
Alex Ku
Ranch Hand

Joined: Jan 15, 2002
Posts: 47
Try this

or much simpler version

kawaii
[ February 12, 2002: Message edited by: kawaii desu ]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
"I want to reverse the string eithout using any temprory variables,String Buffer and arrays."
Impossible. There is no way to do this without using some sort of temporary variable. Once you allow this, there are several possible ways - kawaii's second solution is the most efficient.


"I'm not back." - Bill Harding, Twister
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Also, Prasad - please change your display name to comply with our name policy. "CTS" does not look like it could possibly be a real last name - we ask that everyone use a real name (first and last) for their display name. You can fix this by going to "my profile" and then "Edit/View Profile". Thanks.
Prasad Chelur
Greenhorn

Joined: Feb 12, 2002
Posts: 10
I think the answer u have given is not what i am Expeciting.
Below is my code can u now able to give me a soln.
import java.io.*;
public class preparestr{
String s="Hello World";

public void reversing(){
int i=s.length();
i=i-1;
for(int j=i; j>=0;j--)
{
System.out.println("The reversed string is"+s.charAt(j));
}
}

public static void main(String args[])
{
preparestr ps=new preparestr();
ps.reversing();
}
}
Now i want to change the original information in s to dlrow olleh
jayram
Ranch Hand

Joined: Oct 30, 2000
Posts: 94
Prasad,
As mentioned by other posters its not possible to reverse a string in java as strings are immutable. You need to either use StringBuffer or read your String in reverse order character at a time into an array and then create a "new" string object out of this array. Remember this is still a "new" object.
Your code is not reversing a String.You'r just reading the characters in reverse order and printing them to console but your orginal String object is still unchanged. Kawaii has given code which should work correctly and solve your purpose
Cheers
Jayram
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Hello,
Just for kicks...
Maybe something like this is helpful:

I've made an applet, you could easily change it to an application.
Good Luck,
-Dirk Schreckmann


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Hello Again,
Note: You could also do this:

Effectively "changing" your original string to its own reverse.

But remember, Strings are immutable - you cannot change them. You can "point" a variable name to a new String. That is what has been done here. Here is a link to a discussion involving "immutable" with some good answers
Good Luck,
-Dirk Schreckmann
[ February 14, 2002: Message edited by: Dirk Schreckmann ]
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
OK, OK, OK...
With all of the pieces put together, perhaps this is what you are trying to achieve:
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
...all of which are quivalent to kawaii's second program, arent't they? At least as far as the core algorithm is concerned.
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Originally posted by Jim Yingst:
...all of which are quivalent to kawaii's second program, arent't they? At least as far as the core algorithm is concerned.

Yes they are - but without any extra variables, which seems to have been important to Prasad.
[ February 15, 2002: Message edited by: Dirk Schreckmann ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String Reversing.
 
Similar Threads
String reversing without using String functions...
Reversing a string
String Reversing With code
Dount in K&B exam question
String reversing without using String functions...