[01 Feb 2020] Reverse a string in Java



Reverse a string in Java


This article discusses 5 different ways to reverse a string in Java with examples.
Examples:
string-reverse
Following are some interesting facts about String and StringBuffer classes :
1. Objects of String are immutable.
2. String class in Java does not have reverse() method, however StringBuilder class has built in reverse() method.
3. StringBuilder class do not have toCharArray() method, while String class does have toCharArray() method.
// Java program to ReverseString using ByteArray.
import java.lang.*;
import java.io.*;
import java.util.*;
  
// Class of ReverseString
class ReverseString
{
    public static void main(String[] args)
    {
        String input = "GeeksforGeeks";
  
        // getBytes() method to convert string 
        // into bytes[].
        byte [] strAsByteArray = input.getBytes();
  
        byte [] result =
                   new byte [strAsByteArray.length];
  
        // Store result in reverse order into the
        // result byte[]
        for (int i = 0; i<strAsByteArray.length; i++)
            result[i] = 
             strAsByteArray[strAsByteArray.length-i-1];
  
        System.out.println(new String(result));
    }
}
Output:
skeeGrofskeeG
Using built in reverse() method of the StringBuilder class: String class does not have reverse() method, we need to convert the input string to StringBuilder, which is achieved by using the append method of StringBuilder. After that, print out the characters of the reversed string by scanning from the first till the last index.
// Java program to ReverseString using StringBuilder
import java.lang.*;
import java.io.*;
import java.util.*;
  
// Class of ReverseString
class ReverseString
{
    public static void main(String[] args)
    {
        String input = "Geeks for Geeks";
  
        StringBuilder input1 = new StringBuilder();
  
        // append a string into StringBuilder input1
        input1.append(input);
  
        // reverse StringBuilder input1
        input1 = input1.reverse();
  
        // print reversed String
        System.out.println(input1);
    }
}
Output:
skeeG rof skeeG

// Java program to Reverse a String  by
// converting string to characters  one
// by one
import java.lang.*;
import java.io.*;
import java.util.*;
  
// Class of ReverseString
class ReverseString
{
    public static void main(String[] args)
    {
        String input = "GeeksForGeeks";
  
        // convert String to character array
        // by using toCharArray
        char[] try1 = input.toCharArray();
  
        for (int i = try1.length-1; i>=0; i--)
            System.out.print(try1[i]);
    }
}
Output:
skeeGrofskeeG

// Java program to Reverse a String using ListIterator
import java.lang.*;
import java.io.*;
import java.util.*;
  
// Class of ReverseString
class ReverseString
{
    public static void main(String[] args)
    {
        String input = "Geeks For Geeks";
        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:
skeeG roF skeeG
































































































Comments

Popular posts from this blog

Very Basic Java Programs

[08 Feb 2020] Minimum number of platforms required for a railway station

Interview Questions asked in Goldman sach interview