Results 1 to 16 of 16
  1. #1
    cysquatch is offline Member
    Join Date
    Mar 2010
    Posts
    5
    Rep Power
    0

    Default Reverse a string?

    How can you reverse a string/number without using any loops or existing methods?

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by cysquatch View Post
    How can you reverse a string/number without using any loops or existing methods?
    For Strings have a look at the StringBuilder or StringBuffer classes. Numbers are quite a different cup of tea and a simple recursive method can do the job if the number is an integral number.

    kind regards,

    Jos

  3. #3
    cysquatch is offline Member
    Join Date
    Mar 2010
    Posts
    5
    Rep Power
    0

    Default

    No sweat, thanks for that.

    Also another required tip...can you explain how to read/put the methods and libraries into usable code at all? I know i'm a noob but seeing something such as this: BufferedReader (Java 2 Platform SE v1.4.2)
    I'm a bit unsure of how to put it all into practice.

    Thanks & Sorry :confused:

  4. #4
    cysquatch is offline Member
    Join Date
    Mar 2010
    Posts
    5
    Rep Power
    0

    Default

    I have an idea for how I want to solve my problem:

    Say for example someone enters a 3 digit number, 123
    using the charAt(); function, i'd want to read the above 123 string into a new string in reverse. Is it possible to just say something like:

    charAt(2);
    append to result;
    charAt(1);
    append to result;
    charAt(0);
    append to result;

    I'm just a bit new to this coding area.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by cysquatch View Post
    I have an idea for how I want to solve my problem
    Why don't you give it a try:

    Java Code:
    public class MyClass {
       public static void main(String[] args) {
          StringBuilder sb= new StringBuilder();
          // your code goes here
       }
    }
    compile and run it:

    Java Code:
    javac MyClass.java
    java -cp . MyClass
    ... and see if your idea was correct.

    kind regards,

    Jos

  6. #6
    cysquatch is offline Member
    Join Date
    Mar 2010
    Posts
    5
    Rep Power
    0

    Default

    Not sure if this is right but i think i went a different way.
    I assigned each letter in the string to a char using charAt(x); but am wanting to now assign the characters in each of those, append them to a string.
    The values have been read into the char's correctly, guess I just need this last part to append all the letters to one string, is that a hard thing to do?

  7. #7
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    StringBuilder (Java Platform SE 6)

    No, it's not a hard thing to do.

    -Gary-

  8. #8
    cysquatch is offline Member
    Join Date
    Mar 2010
    Posts
    5
    Rep Power
    0

    Default

    StringBuilder (Java Platform SE 6)

    This isn't really helpful to be as since i'm new i don't understand how to put these instructions into a working example, sorry.

    What I thought i could do is something like this:

    if the numbers/letters are assigned from their character positions, example

    a = number.charAt(5);
    b = number.charAt(4);
    c = number.charAt(3); ...and so on,

    I can use printf and say the string backwards is %c%c%c then insert the arguments, a,b,c but i just want to append the above abc to a string like 'result'.

    Could you explain how this can be done by using some kind of a concat method or something?

  9. #9
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    JosAH has given you a skeleton method above. What I sent should have been a link to the append() method of the StringBuilder class (but I see the link doesn't work well in my browser). You invoke it like this:

    Java Code:
        sb.append(c);
    -Gary-

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by cysquatch View Post
    StringBuilder (Java Platform SE 6)

    This isn't really helpful to be as since i'm new i don't understand how to put these instructions into a working example, sorry.
    Being new to programming doesn't imply you're illiterate does it? Why don't you read what such a StringBuilder can do for you? Spoiler: it can reverse its content (a representation of a String) for you.

    kind regards,

    Jos

  11. #11
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    If you find it hard to connect the dots from the API documentation to a working example (eg because the syntax of Java is new or whatever) then Google is your friend. The first hit for "StringBuilder reverse" looks pretty good when I tried it.

    Of course you have to exercise a certain amount of sense in choosing whether to accept what you read. I was going to say common sense but, to be fair, it isn't common when you're starting. So look for usage examples that are simple, easy to follow and presented on pages with professional layout and language use.

    Understand any code ideas you pick up. That will involve your textbook/teacher/whatever. And, having done your own work, ask here (with code attempt) if things aren't clear or don't give the intended results.

  12. #12
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Now now, let's all take a breather first :)
    Ok, instead of jumping the gun and stare at complicated classes, why don't we try and figure out a simple solution, for such a simple problem? A few lines of recursive code solve this quite nicely, I'll just describe the solution, so the OP can figure out for himself how to put it into code.
    So, to reverse a string, you need to take the first letter, and make it last, and so on and so on. One letter doesn't need to be reversed, so that can be our base case. In short, get the reversed substring without the first character, and append the first character to the back of it. Only substring() and charAt() methods are used and the whole program consists of 3 lines (if you're not counting method headers and main :D).

  13. #13
    programmer_007's Avatar
    programmer_007 is offline Senior Member
    Join Date
    Aug 2009
    Posts
    125
    Rep Power
    0

    Default

    without using any loops or existing methods?
    y r we forgetting this.. rite in the first post. :p

  14. #14
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    I was proposing a recursive method, so no loops, and no existing methods probably means the reverse() method from the string class.

  15. #15
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by m00nchile View Post
    I was proposing a recursive method, so no loops, and no existing methods probably means the reverse() method from the string class.
    The String class doesn't have a reverse() method and you'd have to at least use one other method in your recursive solution so it is not allowed ;-)

    kind regards,

    Jos

  16. #16
    HackerOfDoom is offline Member
    Join Date
    Mar 2010
    Posts
    21
    Rep Power
    0

    Default

    May I ask why you need not to use loops and such? That would be by far the easiest way to reverse a string.

Similar Threads

  1. reverse string split
    By Fittersman in forum Advanced Java
    Replies: 4
    Last Post: 03-09-2010, 12:29 AM
  2. reverse a string with a while loop...
    By OptimusPrime in forum New To Java
    Replies: 9
    Last Post: 12-28-2009, 11:06 PM
  3. Reverse a string not using the substring method
    By kathyla18 in forum New To Java
    Replies: 17
    Last Post: 04-08-2009, 04:08 AM
  4. Reverse and Replace a String in Linear Time
    By colin.cruise in forum New To Java
    Replies: 5
    Last Post: 07-01-2008, 09:02 PM
  5. How to reverse two dimensional
    By masaka in forum New To Java
    Replies: 4
    Last Post: 05-19-2008, 10:02 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
  •