I want to ask how do you reverse just characters in a string? I tried String builder but it reverses the whole string, so the order isn't good in the string
Asked
Active
Viewed 44 times
1 Answers
-1
public static String reverse(String src) {
StringBuilder builder = new StringBuilder();
for (int i = src.length()-1; i > -1; i--) {
builder.append(src.charAt(i));
}
return builder.toString();
}
public static void main(String[] args) {
String src = "Reverse Me";
//easy way
System.out.println(new StringBuilder(src).reverse().toString());
//hard way
System.out.println(reverse(src));
}
Ryan
- 1,666
- 5
- 11
-
-
1@DavidConrad read my solution very carefully. I suggest looking just below the comment that reads "easy eay". – Ryan May 16 '22 at 16:15