• 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 Reversing.

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try this

or much simpler version

kawaii
[ February 12, 2002: Message edited by: kawaii desu ]
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"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.
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, OK, OK...
With all of the pieces put together, perhaps this is what you are trying to achieve:
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
...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
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
bacon. tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic