In Java, you can reverse a string in several ways. Here are three common methods to reverse a string:
String reverse program in java using StringBuilder
public class StringReverseExample {
public static void main(String[] args) {
String original = "Hello, World!";
StringBuilder reversed = new StringBuilder(original).reverse();
String reversedString = reversed.toString();
System.out.println("Reversed string: " + reversedString);
}
}
This program uses the StringBuilder class, which is a mutable sequence of characters. Here’s how it works:
- We start with the original string
"Hello, World!". - We create a
StringBuilderobject calledreversedand initialize it with the original string usingnew StringBuilder(original). - We use the
reverse()method of theStringBuilderclass to reverse the characters in thereversedobject. - Finally, we convert the
StringBuilderback to a regular stringtoString()and store it in thereversedStringvariable. Then, we print the reversed string.
Using a loop to reverse the string character by character:
public class StringReverseExample {
public static void main(String[] args) {
String original = "Hello, World!";
String reversed = reverseString(original);
System.out.println("Reversed string: " + reversed);
}
public static String reverseString(String str) {
StringBuilder reversed = new StringBuilder();
for (int i = str.length() - 1; i >= 0; i--) {
reversed.append(str.charAt(i));
}
return reversed.toString();
}
}
In this program, we reverse the string by iterating through its characters using a loop:
- We start with the original string
"Hello, World!". - We define a method
reverseStringthat takes a stringstras input. - Inside the
reverseStringmethod, we create aStringBuildercallreversedto build the reversed string. - We use a
forloop to iterate through the characters of the input string from the end (length – 1) to the beginning (0). - In each iteration, we append the character at the current position to the
reversedStringBuilder. - Finally, we convert the
StringBuilderto a regular string usingtoString()and return the reversed string.
String reverse program in java Using recursion
public class StringReverseExample {
public static void main(String[] args) {
String original = "Hello, World!";
String reversed = reverseString(original);
System.out.println("Reversed string: " + reversed);
}
public static String reverseString(String str) {
if (str.isEmpty()) {
return str;
}
return reverseString(str.substring(1)) + str.charAt(0);
}
}
This program uses a recursive approach to reverse the string:
- We start with the original string
"Hello, World!". - We define a method
reverseStringthat takes a stringstras input. - Inside the
reverseStringmethod, we have a base case that checks if the input string is empty. If it’s empty, we return the same empty string. - In the recursive case, we call
reverseStringwith a substring of the input stringstr.substring(1), effectively removing the first character, and then we concatenate the first characterstr.charAt(0)to the reversed result. - This process continues recursively until the base case is reached, and the characters are concatenated in reverse order.
These are three different approaches to reversing a string in Java, each with its own advantages and use cases.
The first method using StringBuilder is the most efficient and commonly used for reversing strings.
The second method using a loop is also straightforward and efficient. The third method using recursion is less efficient due to the overhead of recursive function calls but can be useful in certain situations or for educational purposes.
String reverse programs in Java can be done in the above three ways. Hope you learned all things.
Read More
If else nested and ladder if else statement in Java
Registration Form in Java | Student registration form using Java Swing source code