Results 1 to 11 of 11
  1. #1
    marco.c84 is offline Member
    Join Date
    Mar 2009
    Posts
    6
    Rep Power
    0

    Default Reverse A String Without Allocating A New String

    Hi guys,
    how would I reverse a string without allocating a new string or using any temporary variables for holding information?

    Thanks

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,037
    Rep Power
    10

    Default

    What have you tried?

    And I'm pretty sure you're just misusing some vocabulary here. It's going to be impossible to create one String based on another String without allocating a new String, even if you do end up saving that new String to the same variable.

    Hint- Are you allowed to use a StringBuilder?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Reflection and modify the char[] "behind" the string. Not that I actually suggest you try that, but it is possible.

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,037
    Rep Power
    10

    Default

    Quote Originally Posted by masijade View Post
    Reflection and modify the char[] "behind" the string. Not that I actually suggest you try that, but it is possible.
    Hahaha! I would love it if the OP did that and handed it in as the solution.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Well, I would give it an A (for @ss as in <dry>Smart@ss</dry>).

  6. #6
    nap_patague is offline Member
    Join Date
    Mar 2011
    Location
    Bulacan, Philippines
    Posts
    23
    Rep Power
    0

    Default

    String a="abcdefghijklmn";

    a = new StringBuilder(a).reverse();
    Keep Moving Forward

  7. #7
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Quote Originally Posted by nap_patague View Post
    String a="abcdefghijklmn";

    a = new StringBuilder(a).reverse();
    That creates a new String, BTW.

  8. #8
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    Strings are immutable. You can not alter a String, you can only create new ones.

  9. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    If the actual objective is to just display the reversed String then use a reverse loop.

  10. #10
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Do not spoonfeed people code as it hinders their learning.

    Do not communicate privately as this goes against the spirit of sharing information on a forum.

  11. #11
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default

    It's not a spoonfeeder, it's a blasted siglink spammer. I've reported abuse on all 5 unrelated inane responses.

    Chances are the email (? PM ?) if any contains more spam.

    db

Similar Threads

  1. Reverse a string?
    By cysquatch in forum New To Java
    Replies: 15
    Last Post: 03-23-2010, 03:31 AM
  2. reverse string split
    By Fittersman in forum Advanced Java
    Replies: 4
    Last Post: 03-09-2010, 01:29 AM
  3. reverse a string with a while loop...
    By OptimusPrime in forum New To Java
    Replies: 9
    Last Post: 12-29-2009, 12:06 AM
  4. Reverse a string not using the substring method
    By kathyla18 in forum New To Java
    Replies: 17
    Last Post: 04-08-2009, 05:08 AM
  5. Non Allocating String Buffer
    By chrisdb89 in forum New To Java
    Replies: 5
    Last Post: 10-25-2008, 07:57 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •