string reverse method in java code example
Example 1: reverse string java
String string="whatever";
String reverse = new StringBuilder(string).reverse().toString();
System.out.println(reverse);
Example 2: how to reverse a string in java
public class ReverseString {
public static void main(String[] args) {
String s1 = "neelendra";
for(int i=s1.length()-1;i>=0;i--)
{
System.out.print(s1.charAt(i));
}
}
}
Example 3: java string reverse
String rev = new StringBuilder("Your String").reverse().toString();
Example 4: string reverse in java
String str = "Hello";
String reverse(String str){
StringBuilder sb = new StringBuilder();
sb.append(str);
sb.reverse();
return sb.toString();
}
Example 5: Reverse a string in java
class ReverseUsingToCharArrayMethod
{
public static void main(String[] args)
{
String str = "Hello World Java";
char[] chTemp = str.toCharArray();
int left, right = 0;
right = chTemp.length - 1;
for(left = 0; left < right ; left++, right--)
{
char temp = chTemp[left];
chTemp[left] = chTemp[right];
chTemp[right]=temp;
}
for(char c : chTemp)
System.out.print(c);
System.out.println();
}
}
Example 6: reverse string array java
public class ReverseArrayDemo
{
public static void main(String[] args)
{
int[] arrNumbers = new int[]{2, 4, 6, 8, 10};
System.out.println("Given array: ");
for(int a = 0; a < arrNumbers.length; a++)
{
System.out.print(arrNumbers[a] + " ");
}
System.out.println("Reverse array: ");
for(int a = arrNumbers.length - 1; a >= 0; a--)
{
System.out.print(arrNumbers[a] + " ");
}
}
}