Make your program more efficient, using string reverse in java.
A string is a sequence of characters that behave as an object in Java. The string is one of the very common and used data structures after arrays. It’s an object that stores the data in a character array.
To elaborate, just consider a string as a character array wherein you can solve many string-based problems.
To make a string object, you need the java language, String class. Java programming uses UTF -16 to show a string. Strings are immutable therefore their internal state remains constant after the object is entirely made. The string object performs many operations, but the string reverse in Java is the most widely used function.
For this, various methods are used such as manual, inbuilt functions, and some useful classes that operate on the String objects.
Example of String Reverse Program in Java
For eg. HELLO string reverse and give the output as OLLEH
Different Ways to Reverse a String
Since the strings are immutable objects, you need to make another string to reverse them. The string class doesn’t have a reverse method to reverse the string. It has a to CharArray() method to do the reverse.
Here below are some ways to reverse a string as follows;
By Using toCharArray()
The code given below would help to understand how to reverse a string. The toCharArray() method is an approach to reverse a string in Java.
The code also uses the length, which provides the total length of the string variable.
The for loop iterates until the end of the string index is zero.
Code
//ReverseString using CharacterArray.
public static void main(String[] arg) {
// declaring variable
String stringinput = "Independent";
// convert String to character array
// by using toCharArray
char[] resultarray = stringinput.toCharArray();
//iteration
for (int i = resultarray.length - 1; i >= 0; i--)
// print reversed String
System.out.print(resultarray[i]);}
By using the Static method
This method consists of the logic to reverse the string.
Make the object for the class Reverse of a String and call the static method with the object as rev. reverse(str)) bypassing the given string.
import java.util.Scanner;
class ReverseofaString
{public static void main(String[] arg)
{ReverseofaString rev=new ReverseofaString();
Scanner sc=new Scanner(System.in);
System.out.print("Enter a string : ");
String str=sc.nextLine();
System.out.println("Reverse of a String is : "+rev.reverse(str));}
static String reverse(String s)
{String rev="";for(int j=s.length();j>0;--j)
{
rev=rev+(s.charAt(j-1));
}
return rev;
}
}
Using Recursion
A method is also called recursive. In this program reverse(String s) is completely recursive.
Make the object for the class ReverseofaString rev. Read the string entered using sc.nextLine() and save it in the string variable str. Call the reverse method as rev. reverse(str).
import java.util.Scanner;
class ReverseofaString
{
String reverse(String s)
{
if(s.length() == 0)
return " ";
return s.charAt(s.length()-1) + reverse(s.substring(0,s.length()-1));
}
public static void main(String[ ] arg)
{
ReverseofaString rev=new ReverseofaString();
Scanner sc=new Scanner(System.in);
System.out.print("Enter a string : ");
String str=sc.nextLine();
System.out.println("Reverse of a String :"+rev.reverse(str));
}
}
Using While Loop
Here i=length of the given string. The loop iterates until the condition i>0 is false, which means if the length of the string is zero then the cursor terminates the loop.
The loop prints the character of the string which is at the index (i-1) until i>0. Then we would get the reverse of a string.
import java.util.Scanner;
class Reverse
{
public static void main(String[ ] arg)
{
String str;
Scanner scan=new Scanner(System.in);
System.out.print("Enter a string : ");
str=scan.nextLine();
System.out.println("Reverse of a String '"+str+"' is :");
int i=str.length();
while(i>0)
{
System.out.print(str.charAt(i-1));
i--;
}
}
}
Using For Loop
For loop iterates from j=length of the string to j>0.
It prints the character of the string that is at the index (i-1), then we will get the reverse of a string.
import java.util.Scanner;
class ReverseofaString
{
public static void main(String[ ] arg)
{
String str;
char ch;
Scanner sc=new Scanner(System.in);
System.out.print("Enter a string : ");
str=sc.nextLine();
System.out.println("Reverse of a String '"+str+"' is :");
for(int j=str.length();j>0;--j)
{
System.out.print(str.charAt(j-1));
}
}
Using Stringbuilder/StringBuffer
Using StringBuilder.reverse() method to reverse a Java string efficiently. Also, it can also use the StringBuffer.reverse() method. To use StringBuilder is preferred as it’s not synchronized and faster than StringBuffer.
class Main
{
// Method to reverse a string in Java using `StringBuilder`
public static String reverse(String str) {
return new StringBuilder(str).reverse().toString();
}
public static void main(String[] args)
{
String str = "Techie Delight";
// Note that string is immutable in Java
str = reverse(str);
Read Also