-1

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

1 Answers1

-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