How To Reverse String In Java in Different Ways
In this post, we are going to learn how to take String input from the user and then we will make the string reverse in different ways in Java. You can choose any of these programs which is best to understand for you.
- We can use charAt method to extract each character and then we can apply our own logic to reverse the string
- We can also use StringBuffer class. Because this class has a method to reverse an object of StringBuffer class.
- StringBuilder class can be used as there is a reverse method to invert our string.
- Storing the string into a char array, we can reverse the string by using index value in a for loop.
Check If A String Is Palindrome Or Not Ignoring The Case Of Characters In Java
Reverse String in Recursive Java Program With substring() and charAt() Method
import java.util.Scanner; public class Codespeedy { public static void main(String[] args) { Scanner input=new Scanner(System.in); System.out.println("Enter a String: "); String string = input.nextLine(); String reversed = reverse(string); System.out.println("The inverted string is: " + reversed); } public static String reverse(String string) { if (string.isEmpty()) return string; //Recursive Method return reverse(string.substring(1)) + string.charAt(0); } }
Output:
run: Enter a String: Hello CodeSpeedy The inverted string is: ydeepSedoC olleH BUILD SUCCESSFUL (total time: 7 seconds)
A Java Program To Find Duplicate Values In an Array
Algorithm and explanation of this recursive java program to reverse a stringĀ
- Input is taken from the user. (String input)
- reverse method is called, passing the parameter string entered by the user.
- If String is empty just return the empty string
- If String is not empty then return the method recursively, that means in reverse method it will call the method “reverse” itself and it will print the Reversed String finally
return reverse(string.substring(1)) + string.charAt(0);
This is the main algorithm which reverses the string
Reverse String In Java Using StringBuilder Class
import java.util.Scanner; public class CodeSpeedy { public static void main(String[] args) { Scanner input=new Scanner(System.in); System.out.println("Enter a String: "); String string = input.nextLine(); StringBuilder userinput = new StringBuilder(); //Creating object userinput.append(string); //appending string into userinput userinput = userinput.reverse(); //reversing string for (int i=0; i<userinput.length(); i++) System.out.print(userinput.charAt(i)); //printing final output } }
Output
run: Enter a String: Hello CodeSpeedy ydeepSedoC olleH
How to replace String Characters In Java Using replace() Method?
Reverse String In Java Using char array ( .toCharArray() )
the algorithm of this method:
- Take input from the user.
- create a char array and store the string in that char array.
- Run a for loop and print the characters one by one in each iteration ( Using index value the characters will be printed from the last character to the first character)
Java Code
import java.util.Scanner; public class Codespeedy { public static void main(String[] args) { Scanner input=new Scanner(System.in); System.out.println("Enter a String: "); String string = input.nextLine(); char[] result = string.toCharArray(); System.out.println("Reversed String Is:"); for (int i = result.length-1; i>=0; i--) System.out.print(result[i]); } }
Output:
run: Enter a String: Hello CodeSpeedy Reversed String Is: ydeepSedoC olleH
Merge (Combine) Two or More Arrays Into A Single Array In Java
Leave a Reply