Reverse String Without Using Split in Java
To reverse a string without using the split function, we can perform the action in the following ways:
By Using toCharArray() Method
The length keyword is used in the program, and it returns the overall length of the string variable.
The for loop continues until the string index zero ends.
ReverseString.java
import java.io.*;
import java.util.*;
import java.io.*;
public class ReverseString
{
//ReverseString using CharcterArray.
public static void main(String[] args) {
// creating object for the scanner class to take inputs during the run time
Scanner sc=new Scanner(System.in);
System.out.println("Enter the string");
// declaring and initialising variable
String s=sc.nextLine();
System.out.println("The given string is ");
System.out.println(s);
// convert the given string to character array by using toCharArray
char[] result = s.toCharArray();
//iterating through the char array and printing it
for (int j = result.length - 1; j >= 0; j--)
{
// printing the reversed String
System.out.print(result[j]);
}
}
}
Output:
Enter the string
Hellow World
The given string is
Hellow World
dlroW wolleH
Method 2
- The goal is to go through the string's entire length.
- While navigating, extract each character.
- Add each character to the string's front.
RevsersString2.java
import java.util.*;
import java.io.*;
public class RevsersString2
{
public static void main(String[] args) {
// declaring variable
Scanner sc=new Scanner(System.in);
System.out.println("Enter the string");
String s=sc.nextLine();
System.out.println("The given string is ");
System.out.println(s);
String result="";
//iterating through the char array and printing it
for (int i=0; i<s.length(); i++)
{
char ch= s.charAt(i); // extracting each character from the string
result= ch+result; // add each character to the string
}
System.out.println("The revered string is");
System.out.println(result);
}
}
Output:
Enter the string
Java Programming
The given string is
Java Programming
The revered string is
gnimmargorP avaJ
Using Byte Array
- Establish a temporary byte[] with a length equal to the input string.
- Put the bytes (which we obtained using the getBytes() method) into the temporary byte[] in reverse order.
- Make a new String subject using the result's byte[] storage.
ReverseString3.java
import java.util.*;
import java.io.*;
public class ReverseString3
{
public static void main(String[] args) {
// declaring variable
Scanner sc=new Scanner(System.in);
System.out.println("Enter the string");
String s=sc.nextLine();
System.out.println("The given string is ");
System.out.println(s);
byte[] byteArray = s.getBytes();
byte[] result = new byte[byteArray.length];
// Store result in reverse order into the result byte[]
for (int j = 0; j < byteArray.length; j++)
result[j] = byteArray[byteArray.length - j - 1];
System.out.println("The reversed string is");
System.out.println(new String(result));
}
}
Output:
Enter the string
Heloo Everyone
The given string is
Hello Everyone
The reversed string is
enoyrevE olleH
Using StringBuilder Class
Since the String class lacks a reverse() method, we must transform the input string to a StringBuilder instead. This is done by utilizing the StringBuilder's append method. Print the characters of the inverted string after that by scanning through the first through the final index.
ReverseString4.java
import java.util.*;
import java.io.*;
public class ReverseString4
{
public static void main(String[] args) {
// declaring variable
Scanner sc=new Scanner(System.in);
System.out.println("Enter the string");
String s=sc.nextLine();
System.out.println("The given string is ");
System.out.println(s);
StringBuilder s1 = new StringBuilder();
s1.append(s);
s1.reverse();
// printing the reversed String
System.out.println(s1);
}
}
Output:
Enter the string
Java String
The given string is
Java String
gnirtS avaJ
Using While Loop
In this approach, we first create an empty string to hold the reversed string. We then initialize an integer i to the last index of the input string, which is obtained using the length() method. We then enter a while loop that continues as long as i is greater than or equal to 0. Inside the loop, we add the character at the current index i to the reversed string using the charAt() method, and then decrement i. Once the loop has completed, we return the reversed string.
ReverseString5.java
import java.util.*;
import java.io.*;
public class ReverseString5
{
// Main section of the program from where execution begins
public static void main(String[] args) {
// declaring variable
Scanner sc=new Scanner(System.in);
System.out.println("Enter the string");
String s=sc.nextLine();
System.out.println("The given string is ");
System.out.println(s);
int l= s.length();
System.out.println("The reversed string ");
while( l>0)
{
System.out.print(s.charAt(l -1));
l--;
}
}
}
Output:
Enter the string
This is String reverse program
The given string is
This is String reverse program
The reversed string
margorp esrever gnirtS si sihT
Using For Loop
In this approach, we use a for loop to iterate over the indices of the input string in reverse order. We initialize the loop counter i to the last index of the input string, and then continue as long as i is greater than or equal to 0. Inside the loop, we add the character at the current index i to the reversed string using the charAt() method. Finally, once the loop has completed, we return the reversed string.
RevereString.java
Enter the string
This is String reverse program
The given string is
This is String reverse program
The reversed string
margorp esrever gnirtS si sihT
Output
Enter the string
Hello world
The given string is
Hello world
The reversed string
dlrow olleH