String Reverse Logic in Java
In Java, there are numerous ways to reverse a string. StringBuffer, StringBuilder, iteration, and other tools can be used to reverse a string. Let's look at several Java methods for reversing strings.
These interesting details regarding the String and StringBuilder classes:
String objects are immutable.
There is no reverse() method for the String class in Java, although there is one for the StringBuilder class.
While the toCharArray() method is present in the String class, it is absent in the StringBuilder class.
1. The goal is to run the string's length.
2. While navigating, extract each character.
3. Add each character to the string's front.
Str.java
// java program to reverse a word
import java.io.*;
import java.util.Scanner;
class Str {
public static void main (String[] args) {
String s = "Manoj", rev = "" ;
char c;
System.out.print(" Original string is : ");
System.out.println(s);
for (int i=0; i < s.length(); i++)
{
c= s.charAt(i);
S// char by char at each postition
rev = c + rev; // each character is added before the current string.
}
System.out.println("Reversed String is : "+ rev);
}
}
Output:
Original string is : Manoj
Reversed String is : jonaM
Using getBytes() Method
- Make a temporary byte[] that is the same size as the input string.
- Put the bytes (that we obtained using the getBytes() function) into the temporary byte[] in reverse order.
- Create a new String subject using the result's byte[] storage.
Str1.java
// Reverse String using ByteArray in a Java programme.
import java.lang.*;
import java.io.*;
import java.util.*;
// Class Str1
class Str1 {
public static void main(String[] args)
{
String s = "Manoj";
// string to bytes conversion using
// the getBytes() function[].
byte[] byt = s.getBytes();
byte[] ans = new byte[byt.length];
// Store result in reverse order into the
// ans byte[]
for (int i = 0; i < byt.length; i++)
ans[i] = byte[byt.length - i - 1];
System.out.println(new String(ans));
}
}
Output:
jonaM
Using reverse() Method
Since the String class lacks a reverse() method, we must transform the input string to a StringBuilder instead. This is done by utilising the StringBuilder's append method. Print the characters of the inverted string after that by scanning through the first through the final index.
Str2.java
// Java program to ReverseString using StringBuilder
import java.lang.*;
import java.io.*;
import java.util.*;
// Class of Str2
class Str2 {
public static void main(String[] args)
{
String s = "Manoj";
StringBuilder ans = new StringBuilder();
// add a string to the StringBuilder, then
ans.append(s);
// StringBuilder in reverse ans
ans.reverse();
// reverse String to print
System.out.println(ans);
}
}
Output:
jonaM
The user entered the string to be inverted when converting a string to a character array.
1. First, use the Java String class's built-in toCharArray function to convert a String to a character array ().
2. After that, print each character as you scan the string from beginning to end.
Str3.java
// Java programme that reverses a
// string by individually changing
// each character in the string
import java.lang.*;
import java.io.*;
import java.util.*;
// Class of Str3
class Str3 {
public static void main(String[] args)
{
String s = "Manoj";
// With toCharArray, transform a
// String into a character array.
char[] ans = s.toCharArray();
for (int i = ans.length - 1; i >= 0; i--)
System.out.print(ans[i]);
}
}
Output:
jonaM
Using the toCharArray() Function
Use the built-in toCharArray() function of the String Class to convert the input string into a character array. The character array is then simultaneously scanned from the start index (left) and the last index (right) on both sides.
1. Set the right index to -1 for the string's length and the left index to 0.
2. One by one, swap the characters from the start index scanning with the ones from the end index scanning. To go on to the following characters in the character array, increase the left index by 1 (left++) and lower the right index by 1 (right—).
3. Keep going until left is lower than or equal to right.
Str4.java
import java.lang.*;
import java.io.*;
import java.util.*;
class Str4 {
public static void main(String[] args)
{
String s = "Manoj";
char[] arr = s.toCharArray();
int l, r = 0;
r = arr.length - 1;
for (l = 0; l < r; l++, r--) {
char temp = arr[l];
arr[l] = arr[r];
arr[r] = temp;
}
for (char onj : arr)
System.out.print(onj);
System.out.println();
}
}
Output:
jonaM