String Reverse in Java Program | For | Recursion | Function | StringBuilder | StringBuffer | Stream
String reverse in Java program using for loop, recursion, and function.
In this article, you will learn how to reverse in Java program using for loop, recursion, and function.
Example
Enter a string to make reverse::
hello example
The reverse of the string is :: elpmaxe olleh
You should have knowledge of the following topics in Java programming to understand this program:
- Java
java.util.Scanner
package - Java
main()
method - Java
System.out.println()
function - Java
for
loop - Java
Recursion
- Java
Function
Source Code
// String Reverse in Java using For loop
import java.util.Scanner;
public class Main {
// It's the driver function
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter a string to make reverse::\n");
String x=in.nextLine();
// It will print the final output
System.out.print ("The reverse of the string is:: ");
for (int i=(x.length()-1); i>=0; i--) {
System.out.print (x.charAt(i));
}
System.out.println("\n");
}
}
Output
Enter a string to make reverse::
hello example
The reverse of the string is :: elpmaxe olleh
Explanation
In this given program, we have taken input hello example
from the user via the system console. Then we iterate over the given input from the right side to the left side to print the characters.
Then It will be returned the reversed string elpmaxe olleh
the output of the given program.
String Reverse in Java using Recursion
// String Reverse in Java using Recursion
import java.util.Scanner;
public class Main {
// It recursive function will print the characters on the right side
public static int revString(String x, int l) {
if (l>=0) {
System.out.print (x.charAt(l));
l--;
revString(x, l);
}
return 0;
}
// It's the driver function
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter a string to make reverse::\n");
String x=in.nextLine();
// It will print the final output
System.out.print ("The reverse of the string is:: ");
revString(x, x.length()-1);
System.out.println("\n");
}
}
String Reverse in Java using Function
// String Reverse in Java using Function
import java.util.Scanner;
public class Main {
// It will reverse the input string
public static void revString(String x) {
for (int i=(x.length()-1); i>=0; i--) {
System.out.print (x.charAt(i));
}
}
// It's the driver function
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter a string to make reverse::\n");
String x=in.nextLine();
// It will print the final output
System.out.print ("The reverse of the string is:: ");
revString(x);
System.out.println("\n");
}
}
String Reverse in Java using Char Array
// String Reverse in Java using Char Array
import java.util.Scanner;
public class Main {
// It's the driver function
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter a string to make reverse::\n");
String x=in.nextLine();
// it will create a character array of the same size as the input string
int length=x.length();
char[] CharStr=new char[length];
// It will reverse the input string
System.out.print ("The reverse of the string is:: ");
for (int i=(length-1); i>=0; i--) {
CharStr[length-i-1]=x.charAt(i);
}
// It will print the final output
System.out.println(String.copyValueOf(CharStr)+"\n");
}
}
Output
Enter a string to make reverse::
Welcome to w3codeworld
The reverse of the string is:: dlrowedoc3w ot emocleW
String Reverse in Java using Method getBytes()
// String Reverse in Java using Method getBytes()
import java.util.Scanner;
public class Main {
// It's the driver function
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter a string to make reverse::\n");
String x=in.nextLine();
// getBytes() method will convert the input string into bytes[]
byte[] StrByteArray=x.getBytes();
int length=StrByteArray.length;
byte[] Result=new byte[length];
// It will reverse the input string
System.out.print ("The reverse of the string is:: ");
for (int i=(length-1); i>=0; i--) {
Result[length-i-1]=StrByteArray[i];
}
// It will print the final output
System.out.println((new String(Result))+"\n");
}
}
String Reverse in Java using StringBuilder's Built-in Method reverse()
// String Reverse in Java using StringBuilder's Built-in Method reverse()
import java.util.Scanner;
public class Main {
// It's the driver function
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter a string to make reverse::\n");
String x=in.nextLine();
// It will create the instance of the `StringBuilder` class
StringBuilder SBinput=new StringBuilder();
SBinput.append(x); // To append the value of x variable
// It will reverse the input value
SBinput.reverse();
// It will print the final output
System.out.println("The reverse of the string is:: "+SBinput+"\n");
}
}
String Reverse in Java using StringBuffer
// String Reverse in Java using StringBuffer
import java.util.Scanner;
public class Main {
// It's the driver function
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter a string to make reverse::\n");
String x=in.nextLine();
// It will create the instance of the `StringBuffer` class
StringBuffer SBuffInput=new StringBuffer(x);
// It will reverse the input value
SBuffInput.reverse();
// It will print the final output
System.out.println("The reverse of the string is:: "+SBuffInput+"\n");
}
}
Output
Enter a string to make reverse::
Hello w3codeworld
The reverse of the string is:: dlrowedoc3w olleH
String Reverse in Java using Stream
// String Reverse in Java using Stream
import java.util.Scanner;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Main {
// It's the driver function
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter a string to make reverse::\n");
String x=in.nextLine();
// It will reverse the input string
String reverseStr=Stream.of(x)
.map(str -> new StringBuilder(str).reverse())
.collect(Collectors.joining());
// It will print the final output
System.out.println("The reverse of the string is:: "+reverseStr+"\n");
}
}
String Reverse in Java using Method toCharArray()
// String Reverse in Java using Method toCharArray()
import java.util.Scanner;
public class Main {
// It's the driver function
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter a string to make reverse::\n");
String x=in.nextLine();
// it will create a character array of the input string
char[] charArr=x.toCharArray();
int length=charArr.length;
// It will reverse the input string
System.out.print ("The reverse of the string is:: ");
for (int i=(length-1); i>=0; i--) {
System.out.print(charArr[i]);
}
System.out.println("\n");
}
}
Output
Enter a string to make reverse::
Welcome programmers
The reverse of the string is:: sremmargorp emocleW
String Reverse in Java using ArrayList Object | ListIterator
// String Reverse in Java using ArrayList Object | ListIterator
import java.util.*;
import java.util.Scanner;
public class Main {
// It's the driver function
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter a string to make reverse::\n");
String x=in.nextLine();
// it will create a character array of the input string
char[] charArr=x.toCharArray();
List<Character> ArrList=new ArrayList<>();
for (char c:charArr) {
ArrList.add(c);
}
// It will reverse the input string
System.out.print ("The reverse of the string is:: ");
Collections.reverse(ArrList);
ListIterator list = ArrList.listIterator();
while (list.hasNext()) {
System.out.print(list.next());
}
System.out.println("\n");
}
}