In this article we will discuss about to Reverse a String in Java with different different examples. Reversing a String in Java is some tricky. Before going ahead we suggest you to look about String vs StringBuilder vs StringBuffer.
![Reverse a String in Java](http://www.fluttertpoint.in/wp-content/uploads/2024/04/FlutterTPoint-Card-7-1024x576.png)
You should know about these concepts:
- Object of String are immutable
- String class in Java does not have reverse() method.
- StringBuilder class has built-in reverse() method.
- StringBuilder class does not have toCharArray() method.
- While String class does have toCharArray() method.
1. String Reverse by iterating in Java
In this way first we will iterate over the string and use the Char Data type to hold the character. An another String variable will be used to append the character to the beginning of the string.
import java.util.Scanner;
public class ReverseWord {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a word: ");
String word = scanner.nextLine();
String reversedWord = reverseWord(word);
System.out.println("Reversed word: " + reversedWord);
scanner.close();
}
public static String reverseWord(String word) {
String reversed = "";
for (int i = word.length() - 1; i >= 0; i--) {
reversed += word.charAt(i);
}
return reversed;
}
}
//output:
Enter a word: Hello
Reversed word: olleH
2. Using built-in StringBuilder reverse() method.
In this way, we have to convert or store the actual string into the StringBuilder variable, then we can directly reverse the String using the
- The input word is converted to a
StringBuilder
object using its constructor, which takes aString
. - The
reverse()
method is then called on theStringBuilder
object to reverse the string. - Finally, the reversed string is obtained by calling
toString()
on theStringBuilder
public class ReverseString {
public static void main(String[] args) {
String original = "Hello, World!";
String reversed = reverseString(original);
System.out.println("Original string: " + original);
System.out.println("Reversed string: " + reversed);
}
public static String reverseString(String str) {
StringBuilder reversed = new StringBuilder(str);
return reversed.reverse().toString();
}
}
//output:
Original string: Hello, World!
Reversed string: !dlroW ,olleH
3. Using toCharArray() method.
Explanation:
- It prompts the user to enter a word.
- It converts the input word into a character array using
toCharArray()
. - It initializes a new character array (
reversedCharArray
) with the same length as the input word. - It iterates through the characters of the input word array from the last element to the first, adding each character to the new array.
- Finally, it constructs a new string from the reversed character array and returns it.
import java.util.Scanner;
public class ReverseWord {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a word: ");
String word = scanner.nextLine();
String reversedWord = reverseWord(word);
System.out.println("Reversed word: " + reversedWord);
scanner.close();
}
public static String reverseWord(String word) {
char[] charArray = word.toCharArray();
int length = charArray.length;
char[] reversedCharArray = new char[length];
for (int i = length - 1, j = 0; i >= 0; i--, j++) {
reversedCharArray[j] = charArray[i];
}
return new String(reversedCharArray);
}
}
//output:
Enter a word: Hello
Reversed word: olleH
Also see:
Thank you for reaching out us. Please look into more useful tutorials.