Introduction to String Reverse Function in Java
We have a huge list of built-in string functions in the Java programming language. Playing with the strings is very common for a developer or the coder. Sometimes we need to reverse a string. There are various ways we can achieve the same. We can either use the existing built-in functions or create our own custom one to fulfill our business needs. Reversing string means getting it from the last index to the first index. Like, ABCUDE is the reverse of the EDUCBA string. There are many ways we can do the same with built-in functions. We can achieve the same by using the StringBuilder, StringBuffer and StringFormatter also. In this topic, we are going to learn about String Reverse Function in Java, and we will see various examples of string reverse.
How does it work?
We can achieve the string reverse in java using StringBuilder.
We can create an object of the StringBuilder and use the string.reverse() function for the string reverse. Since this is a string function, you need not import any additional library. In the same way, we can use the StringBuffer reverse method to get the reverse of any specified string. Also, we can use our own custom code to get the job done. We need to get the string and loop length through the last index to the 0 to get each character one by one.
Syntax
Using StringBuilder
public StringBuilder reverse();
Using the StringBuilder, we can create an object of this class and use the function reverse() to get the reverse of any string.
StringBuilder sb=new StringBuilder(str);
sb.reverse();
Using StringBuffer
public StringBuffer reverse();
Using the StringBuffer, we can create an object of this class and use the function reverse() to get the reverse of any string.
StringBuffer sb=new StringBuffer (str);
sb.reverse();
We can see everything is similar to StringBuffer and StringBuilder.
Using the charAt() function to read character by character from the last index to the o index.
Examples of String Reverse Function in Java
Let’s see all examples using the above-mentioned thoughts and the syntax.
1. String reverse with StringBuilder
As we have already seen in the syntax about the use of this to reverse a string. We will also see the string’s actual string and the reverse string on the output console.
Code:
public class TestStringReverse {
public static void main(String[] args) {
String str = "Hello India";
String reverse_str = StringFormatter.reverseString(str);
System.out.println("Actual String: "+str);
System.out.println("Reverse String: "+reverse_str);
}
}
class StringFormatter {
public static String reverseString(String str){
StringBuilder sb=new StringBuilder(str);
sb.reverse();
return sb.toString();
}
}
Output:
2. String reverse with StringBuffer
This is very similar to the StringBuilder.
Code:
public class TestStringReverse {
public static void main(String[] args) {
String str = "Hello India 1";
String reverse_str = StringBuffer1.reverseString(str);
System.out.println("Actual String: "+str);
System.out.println("Reverse String: "+reverse_str);
}
}
class StringBuffer1 {
public static String reverseString(String str){
StringBuffer sb=new StringBuffer(str);
sb.reverse();
return sb.toString();
}
}
Output:
3. String reverse with our own custom code
Apart from the above example, we can use our own custom code. String reverse is all about getting the string from right to left. We have a length() function to use for getting the length of the string. Now we have the length of the string. We can convert that string and store it in an array of the character data types after storing that string in an array of characters and getting the length of any specified string. We need to run a look from the last index of this array to the 0 indexes of that character array. We will print that character by character. At the end of this process, we will have the reverse string.
Code:
public class TestStringReverse {
public static void main(String[] args) {
String str = "Hello India 2";
char[] strArray = str.toCharArray();
System.out.println("Actual String: "+str);
System.out.print("Reverse String: ");
for(int i=strArray.length-1 ;i>=0; i--){
System.out.print(strArray[i]);
}
}
}
Output:
4. String reverse with our own custom code
This example is very similar to the above one. In case we don’t want to print in the loop, we can declare a string and append every character to that string. At the end of the loop, we will have the reverse string. This is really an ideal case of using the string reverse as compared to the above one.
Code:
public class TestStringReverse {
public static void main(String[] args) {
String str = "Hello India 1";
char[] strArray = str.toCharArray();
System.out.println("Actual String: "+str);
String reverse_string="";
for(int i=strArray.length-1 ;i>=0; i--){
reverse_string +=strArray[i];
}
System.out.print("Reverse String: "+reverse_string);
}
}
Output:
5. The string entered by the user in runtime
For this, we need to import the java scanner utility as we will be using the scanner to read the user entered string. We will read that entered string in an str variable, and then the other process will remain the same as above mentioned (in example 1).
Code:
import java.util.Scanner;
public class TestStringReverse {
public static void main(String[] args) {
String str = "";
Scanner scan=new Scanner(System.in);
System.out.print("Enter a string : ");
str=scan.nextLine();
String reverse_str = StringBuffer1.reverseString(str);
System.out.println("Actual String: "+str);
System.out.println("Reverse String: "+reverse_str);
}
}
class StringBuffer1 {
public static String reverseString(String str){
StringBuffer sb=new StringBuffer(str);
sb.reverse();
return sb.toString();
}
}
Output:
Conclusion
Java programming language itself comes up with string reverse built-in string functions we can use. We should use the built-in function until and unless we don’t have any specific needs. As we can see, these built-in functions are written smartly and are optimized. We can use any function or algorithm, but we should check the time and the space complexity before using that.
Recommended Articles
This is a guide to String Reverse Function in Java. Here we discuss the basic concept,examples of String Reverse Function in Java, along with the output. You may also have a look at the following articles to learn more –
41 Online Courses | 29 Hands-on Projects | 305+ Hours | Verifiable Certificate of Completion
4.8
View Course
Related Courses