Java Program to reverse a string using StringBuilder
Explanation :
Strings are Objects in Java that are internally supported by a char array. Strings are immutable because arrays are immutable(closed for modification). Every time you make a change to a String, a new String is produced. So that we are using StringBuilder which is mutable. Note: we can use StringBuffer class as well.
Code :
public class ReverseStringBuilder {
public static void main(String[] args) {
/* String is immutable */
String name = "Palindrome";
/* Create StringBuilder(mutable) object */
StringBuilder s1 = new StringBuilder();
/* Using append() and reverse() in StringBuilder */
s1.append(name);
s1 = s1.reverse();
/* Print the reverse */
System.out.println(s1);
}
}
Output :
emordnilaP
Steps :
- Create a String class object and initialize it.
- Create a object of string builder class.
- Use the inbuilt function of string builder append() and reverse().
- Finally print string builder object.
Java Program to reverse a string without using String inbuilt function reverse().
Method 01
Explanation :
The toCharArray() function is used to transform that string into a character array. Following that, we'll use the for loop to run through each character in reverse order, get output for each character.
Code :
public class Reverse {
public static void main(String[] args) {
/* String is immutable */
String name = "Palindrome";
/* Using toCharArray() function */
char[] ch = name.toCharArray();
/* Temp string */
String rev = "";
/* Iterating for loop in reverse to store */
for (int i = ch.length - 1; i >= 0; i--) {
/* Concatenating Strings */
rev += ch[i];
}
/* Print the reverse */
System.out.println(rev);
}
}
Output :
emordnilaP
Steps :
- Create a String class object and initialize it.
- Create a char array and call the toCharArray() function with String object.
- Creating a String class object for temp variable.
- Iterating the for loop in reverse to get each character in reverse order.
- Concatenating each character in Temp variable.
- Finally print Temp.
Method 02
Explanation :
Using the for loop, we printed the string in reverse order. The charAt(index) method, on the other hand, returns the character at any given index. The character will be concatenated after each iteration to reverse the string variable.
Code :
public class ReverseCharAt {
public static void main(String[] args) {
/* String is immutable */
String name = "Palindrome";
/* Temp string */
String rev = "";
/* Iterating for loop in reverse to store */
for (int i = name.length() - 1; i >= 0; i--) {
/* Concatenating Strings */
rev = rev + name.charAt(i);
}
/* Print the reverse */
System.out.println(rev);
}
}
Output :
emordnilaP
Steps :
- Creating a String class object and initialize it.
- Creating a String class object for temp variable.
- Iterating the for loop in reverse to get each character in reverse order.
- Concatenating each character to Temp variable by calling charAt() function.
- Finally print Temp.
Special Note: The reverse function is familiar to you but the motive is to introduce the possibility of StringBuilder and how you can optimize code without a reverse function. Hope this will help someone in future!! I welcome your feedback and solutions-oriented suggestions, thank you for your time
Happy Coding❤️❤️❤️
Top comments (0)