In this post we tell you most popular five method to reverse string in java programming.
firstly you see Converting String into Bytes: getBytes() method is used to convert the input string into bytes[].
Methods:
1. Create a temporary byte[] of length equal
to the length of the input string.
2. Store the bytes (which we get by using
getBytes() method) in reverse order into
the temporary byte[] .
3. Create a new String abject using byte[] to
store result.
import java.lang.*;
import java.io.*;
import java.util.*;
// Class of ReverseString
class ReverseString
{
public static void main(String[] args)
{
String input = "internationalyouthacuity";
// 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));
}
}
Using built in reverse() methods of the StringBuilder class: String class does not have reverse() method, we need to convert the input string to StringBuilder, which is achieve by using the append method of StringBuilder. After that, print out the characters of the reverse string by scanning from the first till the last index.
import java.lang.*;
import java.io.*;
import java.util.*;
// Class of ReverseString
class ReverseString
{
public static void main(String[] args)
{
String input = "internationalyouthacuity blogger";
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);
}
}
Converting String to character array method: The user input the string to be reverse.
Methods:
1. First, convert String to character array
by using the built in Java String class
method toCharArray().
2. Then, scan the string from end to start,
and print the character 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 = "internationalyouthacuity blogger";
// 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]);
}
}
Convert the input string into character array by using the toCharArray(): Convert the input string into characters array by using the toCharArray() – built in methods of the String Class. Then, scan the character array from both sides i.e from the start index (left) as well as from last index(right) simultaneously.
1.Set the left index equal to 0 and right
index equal to the length of the string .
2. Swap the characters of the start index
scanning with the last index scanning
one by one. After that, increase the left
index by 1 (left++) and decrease the right
by 1 i.e., (right--) to move on to the next
characters in the character array .
3. Continue till left is less than or equal to the right.
import java.lang.*;
import java.io.*;
import java.util.*;
// Class of ReverseString
class ReverseString
{
public static void main(String[] args)
{
String input = "internationalyouthacuity blogger";
char[] temparray = input.toCharArray();
int left, right=0;
right = temparray.length-1;
for (left=0; left < right ; left++ ,right--)
{
// Swap values of left and right
char temp = temparray[left];
temparray[left] = temparray[right];
temparray[right]=temp;
}
for (char c : temparray)
System.out.print(c);
System.out.println();
}
}
Using ArrayList objects: Convert the input string into the characters array by using toCharArray() built in methods. Then, add the characters of the array into the ArrayList object. Java also has built in reverse() methods for the Collections class. Since Collections class reverse() method takes a list object , to reverse the list , we will pass the LinkedList object which is a type of list of characters.
1. We copy String contents to an object
of ArrayList.
1. We create a ListIterator object by using
the listIterator() method on the LinkedList
object.
2. ListIterator object is used to iterate over
the list.
3. ListIterator object helps us to iterate
over the reversed list and print it one
by one to the output screen.
// 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 = "internationalyouthacuity blogger";
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());
}
}
0 comments:
Post a Comment