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
1 2 3 4 5 6 7 8 | 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
StringBuilder
object calledreversed
and initialize it with the original string usingnew StringBuilder(original)
. - We use the
reverse()
method of theStringBuilder
class to reverse the characters in thereversed
object. - Finally, we convert the
StringBuilder
back to a regular stringtoString()
and store it in thereversedString
variable. Then, we print the reversed string.
Using a loop to reverse the string character by character:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | 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
reverseString
that takes a stringstr
as input. - Inside the
reverseString
method, we create aStringBuilder
callreversed
to build the reversed string. - We use a
for
loop 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
reversed
StringBuilder
. - Finally, we convert the
StringBuilder
to a regular string usingtoString()
and return the reversed string.
String reverse program in java Using recursion
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | 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
reverseString
that takes a stringstr
as input. - Inside the
reverseString
method, 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
reverseString
with 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