Java – String Reverse

In this article, you will be learning about string reverse and the different methods to reverse a string in Java.

Java supports numerous methods to reverse a string. You can reverse String using StringBuffer, StringBuilder, iteration, etc. Let’s see the ways to reverse String in Java.

ByteArray method

Sample Java program to ReverseString using ByteArray.

import java.lang.*;
import java.io.*;
import java.util.*;
 public class Main {
    public static void main(String[] args)
    {
        String input = "CodersEditor";
         byte[] strAsByteArray = input.getBytes();
         byte[] result = new byte[strAsByteArray.length];
         for (int i = 0; i < strAsByteArray.length; i++)
            result[i] = strAsByteArray[strAsByteArray.length - i - 1];
 
        System.out.println(new String(result));
    }
}

Output

rotidesredoc

StringBuilder

In stringbuilder method, a string is reversed by using the append method of StringBuilder class.

Sample program to ReverseString using StringBuilder

import java.lang.*;
import java.io.*;
import java.util.*;
public class Main {
    public static void main(String[] args)
    {
        String input = "Coders Editor";
 
        StringBuilder input1 = new StringBuilder();
        input1.append(input);
        input1.reverse();
        System.out.println(input1);
    }
}

Output

Rotide sredoc

Reverse a String by  converting string to characters  one by one

Sample program for Reversing  a String  by converting string to characters  one  by one

import java.lang.*;
import java.io.*;
import java.util.*;
public class Main {
    public static void main(String[] args)
    {
        String input = "Coders Editor";
         char[] try1 = input.toCharArray();
         for (int i = try1.length - 1; i >= 0; i--)
            System.out.print(try1[i]);
    }
}

Output

Rotide sredoc

Swapping Method

In this method, the input string is first Converted into a character array by using the toCharArray() – built-in method of the String Class.

Sample program for reversing a string by swapping method

import java.lang.*;
import java.io.*;
import java.util.*;
public class Main {
    public static void main(String[] args)
    {
        String input = "Coders Editor";
        char[] temparray = input.toCharArray();
        int left, right = 0;
        right = temparray.length - 1;
        for (left = 0; left < right; left++, right--) {
            char temp = temparray[left];
            temparray[left] = temparray[right];
            temparray[right] = temp;
        }
 
        for (char c : temparray)
            System.out.print(c);
        System.out.println();
    }
}

Output

Rotide sredoc

ListIterator Method

Java also has built in reverse() method for the Collections class. As we know the Collections class reverse() method takes a list object, to reverse the list, we will pass the ArrayList object which is a type of list of characters. For the ArrayList, the input string is converted into ArrayList.

Sample program to Reverse a String using ListIterator

import java.lang.*;
import java.io.*;
import java.util.*;
public class Main {
    public static void main(String[] args)
    {
        String input = "Coders Editor";
        char[] hello = input.toCharArray();
        List<Character> trial1 = new ArrayList();
 
        for (char c : hello)
            trial1.add(c);
 
        Collections.reverse(trial1);
        ListIterator li = trial1.listIterator();
        while (li.hasNext())
            System.out.print(li.next());
    }
}

Output

Rotide sredoc

String Buffer method

Sample program using stringbuffer to reverse a string

import java.lang.*;
import java.io.*;
import java.util.*;
 
public class Test {
    public static void main(String[] args)
    {
        String str = "Coders Editor";
         StringBuffer sbr = new StringBuffer(str);
        sbr.reverse();
        System.out.println(sbr);
    }
}

Output

Rotide sredoc
close

Copy and paste this code to display the image on your site

%d bloggers like this: