Eric Brooks
0
Q:

reverse string array java

public class ReverseString {
    public static void main(String[] args) {
        String s1 = "neelendra";
        for(int i=s1.length()-1;i>=0;i--)
            {
                System.out.print(s1.charAt(i));
            }
    }
}
13
// reverse array

for(int i=intArray.length-1;i>=0;i--)
         System.out.print(intArray[i] + "  ");
2
// 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();
   }
}
1
// reverse a string using character array
class ReverseUsingCharacterArray
{
   public static void main(String[] args)
   {
      String str = "HelloWorldJava";
      char[] ch = str.toCharArray();
      for(int a = ch.length - 1; a >= 0; a--)
         System.out.print(ch[a]);
   }
}
1
// reverse a string using reverse() method of StringBuilder class
class ReverseUsingReverseMethod
{
   public static void main(String[] args)
   {
      String str = "Hello world Java";
      StringBuilder sb = new StringBuilder();
      // append string to StringBuilder
      sb.append(str);
      sb = sb.reverse();
      // printing reversed String
      System.out.println(sb);
   }
}
1
int length = array.length;
		for(int i=0;i<length/2;i++) {
			int swap = array[i];
			array[i] = array[length-i-1];
			array[length-i-1] = swap;
		}
or
Collections.reverse(Arrays.asList(array)); 
5
// reverse a string using ByteArray
class ReverseStringByteArray
{
   public static void main(String[] args)
   {
      String input = "HelloWorld";
      // getBytes() method to convert string into bytes[].
      byte[] strByteArray = input.getBytes();
      byte[] output = new byte[strByteArray.length];
      // store output in reverse order
      for(int a = 0; a < strByteArray.length; a++)
         output[a] = strByteArray[strByteArray.length - a - 1];
      System.out.println(new String(output));
   }
}
1
String str = "Hello";
String reverse(String str){
  StringBuilder sb = new StringBuilder();
  sb.append(str);
  sb.reverse();
  return sb.toString();
}
9
As we know that String is immutable. String class do not have reverse() method.
2
//java program to reverse array using for loop
public class ReverseArrayDemo 
{
   public static void main(String[] args) 
   {
      int[] arrNumbers = new int[]{2, 4, 6, 8, 10};  
      System.out.println("Given array: ");  
      for(int a = 0; a < arrNumbers.length; a++)
      {
         System.out.print(arrNumbers[a] + " ");
      }
      System.out.println("Reverse array: ");
      // looping array in reverse order
      for(int a = arrNumbers.length - 1; a >= 0; a--) 
      {  
         System.out.print(arrNumbers[a] + " ");  
      }
   }
}
1

New to Communities?

Join the community