Java Online Compiler
Example: String Reverse in Java using Method getBytes()
C
C++
C#
Java
Python
PHP
Main.java
STDIN
Run
// 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"); } }
Welcome to w3codeworld
Output
Clear
ADVERTISEMENTS