how to reverse a string in java using inbuilt function code example
Example 1: reverse string java
String string="whatever";
String reverse = new StringBuilder(string).reverse().toString();
System.out.println(reverse);
Example 2: java string reverse
String rev = new StringBuilder("Your String").reverse().toString();
Example 3: Reverse a string in java
class ReverseUsingReverseMethod
{
public static void main(String[] args)
{
String str = "Hello world Java";
StringBuilder sb = new StringBuilder();
sb.append(str);
sb = sb.reverse();
System.out.println(sb);
}
}
Example 4: Reverse a string in java
class ReverseStringByteArray
{
public static void main(String[] args)
{
String input = "HelloWorld";
byte[] strByteArray = input.getBytes();
byte[] output = new byte[strByteArray.length];
for(int a = 0; a < strByteArray.length; a++)
output[a] = strByteArray[strByteArray.length - a - 1];
System.out.println(new String(output));
}
}
Example 5: reverse a string in java using input
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter the String:");
char[] letters = input.nextLine().toCharArray();
System.out.println(" Reverse String");
for(int i = letters.length -1; i >= 0; i--){
System.out.print(letters[i]);
}
}
}