reverse a string in java inplace code example
Example 1: reverse string java
String string="whatever";
String reverse = new StringBuilder(string).reverse().toString();
System.out.println(reverse);
Example 2: reverse a string in java
public static String reverse(String str) {
char[] oldCharArray = str.toCharArray();
char[] newCharArray= new char[oldCharArray.length];
int currentChar = oldCharArray.length-1;
for (char c : oldCharArray) {
newCharArray[currentChar] = c;
currentChar--;
}
return new String(newCharArray);
}