Answers for "The reverse method should take a string as an argument and return a new reversed version of the string."

1

Reverse a string in java

// reverse a string using toCharArray() method
class ReverseUsingToCharArrayMethod
{
   public static void main(String[] args)
   {
      String str = "Hello World Java";
      char[] chTemp = str.toCharArray();
      int left, right = 0;
      right = chTemp.length - 1;
      for(left = 0; left < right ; left++, right--)
      {
         // swap values
         char temp = chTemp[left];
         chTemp[left] = chTemp[right];
         chTemp[right]=temp;
      }
      for(char c : chTemp)
         System.out.print(c);
      System.out.println();
   }
}
Posted by: Guest on December-07-2020

Code answers related to "The reverse method should take a string as an argument and return a new reversed version of the string."

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language