Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

How do I reverse the order of a string in java?

If string was equal to "puppy" I want my program to output "yppup"

Really stuck on this last question :s

2 Answers

Relevance
  • 10 years ago
    Favorite Answer

    public static String reverseString(String s)

    {

    return new StringBuffer(s).reverse().toString();

    }

    or

    void reverseString(String str)

    {

    for(int i = str.length()-1; i >= 0; i--)

    System.out.print(str.charAt(i));

    }

  • ?
    Lv 7
    10 years ago

    One approach would be to create a char array the same length as the String, and then fill every element with the characters in the String, starting with the last one and ending with the first one.

Still have questions? Get your answers by asking now.