aspose file tools*
The moose likes Beginning Java and the fly likes reverse a string without a temp string? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "reverse a string without a temp string?" Watch "reverse a string without a temp string?" New topic
Author

reverse a string without a temp string?

Jesse Crockett
Ranch Hand

Joined: Feb 03, 2005
Posts: 129
i'm having trouble reversing a string. I can't use another string as a temp.



that's the best i can do, which makes "world" "worow"
Rusty Shackleford
Ranch Hand

Joined: Jan 03, 2006
Posts: 490
Try using a second char. That way you have both chars needed, and then swap them. You can do this with a single for loop. That will keep this simple. The best way is to perform the swaps on paper and come up with an algorithm.

BTW an array of characters is not the same thing as a Java String and the term function does not exist in Java. That is kind of an odd java assignment, it is very C like.
[ July 15, 2006: Message edited by: Rusty Shackleford ]

"Computer science is no more about computers than astronomy is about telescopes" - Edsger Dijkstra
George Manoj
Greenhorn

Joined: Jul 15, 2006
Posts: 1
[ homework solution deleted - Jim ]

Hope this helps
[ July 15, 2006: Message edited by: Jim Yingst ]
Rusty Shackleford
Ranch Hand

Joined: Jan 03, 2006
Posts: 490
how does giving code help?
Jeroen T Wenting
Ranch Hand

Joined: Apr 21, 2006
Posts: 1847
especially code that's far too complex.
Of course now the OP can turn in his homework and not get a bad grade for a while longer.
Keep that up long enough and he may be able to gather enough good grades that he can afford to fail his final exams and still have a passing grade on average...
He won't have learned anything, but will be able to show people a piece of paper saying he has.
Luckily this OP at least tried and failed, rather than just quoting his assignment, so I assume he does have an interest.

The entire exercise can be performed with a single line of code.
The API docs will tell you the exact operation to execute if you look through them with a little bit of intelligence.
Look for StringBuilder (or StringBuffer if you're having to use an older version of the language).


42
Rusty Shackleford
Ranch Hand

Joined: Jan 03, 2006
Posts: 490
If you cant use string, you likely can't use StringBuilder either.

Assignments like this usually are looking for a solution using basic elements, loops, array iteration, things like that.

Be very careful when iterating and swapping, don't iterate to 'far', or you will have done nothing, and taking more time to do nothing then you might like
Aum Tao
Ranch Hand

Joined: Feb 14, 2006
Posts: 210
The problem with your code is that the characters 'l' and 'd' are getting lost. So, as mentioned earlier, you will need to provide another character and swap them.


SCJP 1.4 85%
Adam Nace
Ranch Hand

Joined: Jul 17, 2006
Posts: 117
Think about what the actual process of reversing a string is:

  • Remove the Character at the End.
  • Place that Character at position 1.
  • Remove the Character that is now at the End.
  • Place that Character at position 2.
  • Remove the Character that is now at the End.
  • Place that Character at position 3.
  • Etc. until reaching the length of the String.



  • One thing to note, however, is that the String class is immutable. When you remove a character, or insert a character, behind the scenes, it actually IS creating temporary, intermediate strings (or rather, StringBuffers), and actually returning a brand new reference to those strings. So the object that your variable refers to will change once for each character in the string.

    I assume, however, that this does not violate your requirements, since the String class is purposely immutable, and I know of no way to hack that.

    Hope this helps!

    - Adam
    Justin Fox
    Ranch Hand

    Joined: Jan 24, 2006
    Posts: 802
    dude, just use a char[] like you did, and just swap the firt and last chars, then increment the first, and decrement the last, then do it again, until the first <= last..

    so the code would look like:


    justin


    You down with OOP? Yeah you know me!
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: reverse a string without a temp string?