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}Algorithm to reverse an array using third variable :
- START
- Initialize an array with values
- Take two variables left and right with left containing 0 and right containing rightmost index of array.
- Take a new variable as temp.
- Swap the values of left indices with the right indices using temp.
- Increment the left variable and decrement the right variable value.
- Repeat the steps from 4 to 5 till left is less than right.
- Print reverse array.
- 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
- START
- Initialize the String Array with values
- Take two variables left and right with left containing 0 and right containing rightmost index of array.
- Take an new variable as temp.
- Swap the values of left indices with the right indices using temp.
- Increment the left variable and decrement the right variable value.
- Repeat the steps from 4 to 5 till left is less than right.
- Print reverse array.
- END
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]
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
Arrays
- Introduction to Arrays – C | C++ | Java
- Introduction to 2-D Arrays – C | C++ | Java
- Sorting of Array – C | C++ | Java
- Array Rotation – C | C++ | Java
- Reverse an array or string- C | C++ | Java
- Find pairs in array with given sum – C | C++ | Java
- Sort the array in Waveform – C | C++ | Java
- Majority Element in Array – C | C++ | Java
- Boyer-Moore’s Voting Algorithm – C | C++ | Java
- K-pairs with smallest sum in 2 arrays – C | C++ | Java
- Largest Sum Contigous SubArray – C | C++ | Java
- Maximum Average Sub-array of K length – C | C++ | Java
- Size of sub-array with max sum – C | C++ | Java
- Sub-array with given sum – C | C++ | Java
- Triplet that sum to a given value – C | C++ | Java
- Segregate 0’s and 1’s in array – C | C++ | Java
- Segregate 0’s 1’s and 2’s in array – C | C++ | Java
- Sort elements in array by frequency – C | C++ | Java
- Finding pythagorean triplets in an array – C | C++ | Java
- Reorder array using given indexes – C | C++ | Java
- Merging two sorted arrays – C | C++ | Java
- Minimum number of Merge Operations to make an Array Palindrome – C | C++ | Java
- Find Zeros to be Flipped so that number of Consecutive 1’s is maximized – C | C++ | Java
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);
}
}
}
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));
}
}
}
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]);
}
}
}
abc
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);
}
}