This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

reverse a string without a temp string?

 
Jesse Crockett
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 490
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
George Manoj
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[ homework solution deleted - Jim ]

Hope this helps
[ July 15, 2006: Message edited by: Jim Yingst ]
 
Rusty Shackleford
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how does giving code help?
 
Jeroen T Wenting
Ranch Hand
Posts: 1847
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Rusty Shackleford
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 210
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Adam Nace
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    Posts: 802
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic