Reverse an Array or String

How To Reverse Integer Or String Array In Java with Example

There are many ways we can perform how to reverse an array in java with example. Reversing an array is an simple question. Let understand the question through the example Input    : {2, 4, 6, 8, 10} Output : {10, 8, 6, 4, 2}
Reverse an Array or String in Java

Algorithm to reverse an array using third variable :

  1. START
  2. Initialize an array with values
  3. Take two variables left and right with left containing 0 and right containing rightmost index of array.
  4. Take a new variable as temp.
  5. Swap the values of left indices with the right indices using temp.
  6. Increment the left variable and decrement the right variable value.
  7. Repeat the steps from 4 to 5 till left is less than right.
  8. Print reverse array.
  9. END
Run
import java.util.*;
public class Main
{
    public static void main (String[] args) 
    {  
        // Given input array 
        int[] inputArray = {2,4,6,8,10}; 
        // Print array before reverse  
        System.out.println("Array without reverse : " + Arrays.toString(inputArray)); 
        // Calling method to swap elements 
        reverse(inputArray);
    }
    public static void reverse(int[] inputArray)
    {
        for (int left = 0, right = inputArray.length - 1; left < right; left++, right--)
        {
            // swap the values at the left and right indices
            int temp = inputArray[left];
            inputArray[left]  = inputArray[right];
            inputArray[right] = temp;
        }
        // Printing the Array after reverse  
        System.out.print("Reverse Array :");
        for(int i : inputArray)
        {
            System.out.print(" "+i);
        }
    }
}
OUTPUT:

Array without reverse : [2, 4, 6, 8, 10]
Reverse Array : 10 8 6 4 2

Algorithm to Reverse a String in JAVA

  1. START
  2. Initialize the String Array with values
  3. Take two variables left and right with left containing 0 and right containing rightmost index of array.
  4. Take an new variable as temp.
  5. Swap the values of left indices with the right indices using temp.
  6. Increment the left variable and decrement the right variable value.
  7. Repeat the steps from 4 to 5 till left is less than right.
  8. Print reverse array.
  9. END
Reversing the elements of String Array
Run
import java.util.*;
public class Main
{
    public static void main (String[] args) 
    {
        // Given input array 
        String [] inputArray = {"India", "UK", "Pakistan", "Australia"};
        // Print array before reverse  
        System.out.println("Initial String array : " + Arrays.toString(inputArray)); 
        // Calling method to swap elements 
        reverse(inputArray);
    }
    public static void reverse(String [] inputArray)
    {
        for(int left = 0, right = inputArray.length - 1;left < right; left++, right--)
        {
            // swap the values at the left and right indices
            String temp = inputArray[left];
            inputArray[left]  = inputArray[right];
            inputArray[right] = temp;
        }
        // Printing the Array after reverse  
        System.out.print("Reverse Array : " + Arrays.toString(inputArray)); 
    }
}
OUTPUT:
Initial String array :  [India, UK, Pakistan, Australia]
Reverse Array :   [Australia, Pakistan, UK, India]

Prime Course Trailer

Related Banners

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

Arrays

  • Introduction to Arrays – CC++ | Java
  • Introduction to 2-D Arrays – CC++ Java
  • Sorting of Array – C | C++ | Java
  • Array Rotation – CC++ | Java
  • Reverse an array or string- CC++ | Java
  • Find pairs in array with given sum – CC++ | Java
  • Sort the array in Waveform – CC++ | Java
  • Majority Element in Array – CC++ | Java
  • Boyer-Moore’s Voting Algorithm – CC++ | Java
  • K-pairs with smallest sum in 2 arrays – C | C++ | Java
  • Largest Sum Contigous SubArray – CC++ | Java
  • Maximum Average Sub-array of K length – CC++ | Java
  • Size of sub-array with max sum – CC++ | Java
  • Sub-array with given sum – CC++ | Java
  • Triplet that sum to a given value – CC++ | Java
  • Segregate 0’s and 1’s in array – CC++ | Java
  • Segregate 0’s 1’s and 2’s in array – CC++ | Java
  • Sort elements in array by frequency – CC++ | Java
  • Finding pythagorean triplets in an array – CC++ | Java
  • Reorder array using given indexes – CC++ | Java
  • Merging two sorted arrays – CC++ | Java
  • Minimum number of Merge Operations to make an Array Palindrome – CC++ | Java
  • Find Zeros to be Flipped so that number of Consecutive 1’s is maximized – CC++ | Java

5 comments on “Reverse an Array or String”


  • Shivam

    import java.util.*;
    public class shivam{
    public static void main(String []shivam){
    Scanner sc= new Scanner(System.in);
    String s=sc.nextLine();
    char []ch=new char[s.length()];
    int j=0;
    for(int i=s.length()-1;i>=0;i–){
    ch[j]=s.charAt(i);
    j++;
    }
    s=new String(ch);
    System.out.print(s);
    }
    }
    }


  • manali

    import java.util.Arrays;
    import java.util.Scanner;

    public class ReverseString {
    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println(“Enter size of array:”);
    int size = sc.nextInt();
    int arr[]=new int[size];
    System.out.println(“Enter array:”);
    for (int i = 0; i =0;i–) {
    System.out.println(arr[i]);
    //System.out.println(Arrays.toString(arr));
    }
    }
    }


  • nilesh

    public class ArrayPrint {

    public static void main(String[] args) {

    int arr[]={10,20,45,75,8,2,6,12,8,7,5,1};

    System.out.println(“array element revers orders”);
    for (int k = arr.length-1;k>=0; k–) {

    System.out.println(arr[k]);

    }
    }

    }


    • nilesh

      public class ArrayPrint {

      public static void main(String[] args) {
      // TODO Auto-generated method stub

      String str2=”abc”;
      String revers=””;

      for (int i=str2.length()-1;i>=0; i–) {
      revers=revers+str2.charAt(i);
      }
      System.out.println(revers);

      }

      }