This discussion is archived
1 2 Previous Next 15 Replies Latest reply: Oct 7, 2008 3:46 AM by 800468 RSS

how to reverse a string?

843785 Newbie
Currently Being Moderated
im having trouble reversing a string
String main = "penut";
    System.out.print(main);
   String reverse = "";
    for (int i=0; i<=main.length(); i++) { 
           //reverse
    }
  • 1. Re: how to reverse a string?
    800282 Newbie
    Currently Being Moderated
    vizzle wrote:
    im having trouble reversing a string
    String main = "penut";
    System.out.print(main);
    String reverse = "";
    for (int i=0; i<=main.length(); i++) { 
    //reverse
    }
    Have a look at the charAt(...) method from the String class:
    [http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html]
  • 2. Re: how to reverse a string?
    843785 Newbie
    Currently Being Moderated
    would it be something along the line of


        String main = "penut";    
        System.out.print(main);
        char reverse;
     
        for (int i=main.length(); i>=0; i-- ) { 
            System.out.print(i);
             reverse = main.charAt(i);
            System.out.print(reverse);
        }
       
  • 3. Re: how to reverse a string?
    800282 Newbie
    Currently Being Moderated
    vizzle wrote:
    would it be something along the line of
    ...
    Try it.
  • 4. Re: how to reverse a string?
    800322 Newbie
    Currently Being Moderated
    "reverse java string" gets countless results in Google.
  • 5. Re: how to reverse a string?
    843785 Newbie
    Currently Being Moderated
    CeciNEstPasUnProgrammeur wrote:
    "reverse java string" gets countless results in Google.
    Liar.
    That phrase, in quotes, returns 6 results.
    Without quotes it returns 204,000.

    Both are quite countable.

    ;-)
  • 6. Re: how to reverse a string?
    843785 Newbie
    Currently Being Moderated
    Navy_Coder wrote:
    Both are quite countable.
    I get [&#8501;o|http://en.wikipedia.org/wiki/Aleph_null] hits myself.
  • 7. Re: how to reverse a string?
    843785 Newbie
    Currently Being Moderated
    Encephalopathic wrote:
    Navy_Coder wrote:
    Both are quite countable.
    I get [&#8501;o|http://en.wikipedia.org/wiki/Aleph_null] hits myself.
    BRILLIANT!
  • 8. Re: how to reverse a string?
    843785 Newbie
    Currently Being Moderated
    hi friend
    you are trying to reverse a string in java like c/c++.

    java provides built in method of reversing a string

    but you have to use StringBuffer instead of String because String is immutable where as StringBuffer is mutable i.e. can be modified.

    try :


    StringBuffer main = "penut";
    System.out.print("String Before : " + main);
    main.reverse();
    System.out.print("Reversed String : " + main);
  • 9. Re: how to reverse a string?
    3004 Newbie
    Currently Being Moderated
    Hardik.Mishra wrote:
    try :


    StringBuffer main = "penut";
    System.out.print("String Before : " + main);
    main.reverse();
    System.out.print("Reversed String : " + main);
    Why don't you try it first, so you can see that it won't work before posting it.
  • 10. Re: how to reverse a string?
    PhHein Guru Moderator
    Currently Being Moderated
    jverd wrote:
    Why don't you try it first, so you can see that it won't work before posting it.
    Maybe he's got that brand new Java 7 compiler with lenient flag?
  • 11. Re: how to reverse a string?
    796215 Newbie
    Currently Being Moderated
    Hardik.Mishra wrote:
    hi friend
    you are trying to reverse a string in java like c/c++.

    java provides built in method of reversing a string

    but you have to use StringBuffer instead of String because String is immutable where as StringBuffer is mutable i.e. can be modified.

    try :


    StringBuffer main = "penut";
    System.out.print("String Before : " + main);
    main.reverse();
    System.out.print("Reversed String : " + main);
    Hi mishra..

    The following statement is wrong..
    StringBuffer main = "penut" 
    you need to test your program before posting it..and make your fundamental clear before posting..

    and why don't you use code tag to post codes..?
  • 12. Re: how to reverse a string?
    796215 Newbie
    Currently Being Moderated
    vizzle wrote:
    im having trouble reversing a string
    String main = "penut";
    System.out.print(main);
    String reverse = "";
    for (int i=0; i<=main.length(); i++) { 
    //reverse
    }
    hi vizzle..

    see the following links

    [http://www.java2s.com/Code/Java/Language-Basics/ReverseStringTest.htm|http://www.java2s.com/Code/Java/Language-Basics/ReverseStringTest.htm]


    [http://www.java-examples.com/java-string-reverse-example|http://www.java-examples.com/java-string-reverse-example]
  • 13. Re: how to reverse a string?
    843785 Newbie
    Currently Being Moderated
    If you don't want a spoon-feed, skip my post.
    .
    .
    .
    . (to prevent u from seeing code)
    .
    .
    .
    .
            /**
          * Reverses a string. For example: <br>
          * <code>reverse("abc ")</code>
          * will return <code>" cba"</code>
          * @param s - The string to be reversed
          * @return The reversed string
          */
         private static String reverse(String s){
              String reversedText = "";
              for(int i=s.length()-1;i>=0;i--){
                   reversedText += s.charAt(i);
              }
              return reversedText;
         }
    
    //in some method you want
    String s = "asdf";
    s = reverse(s);
    Azriel~
  • 14. Re: how to reverse a string?
    800468 Newbie
    Currently Being Moderated
    StringBuilder main = new StringBuilder("penut");
            System.out.print("String Before : " + main);
            main.reverse();
            System.out.print("Reversed String : " + main);
    The above code will work for you..
1 2 Previous Next