Songsang
package net.javaguides.corejava.string; /** * * @author Ramesh Fadatare * */ public class ReverseStringWithSwaps { public static void main(String[] args) { ReverseStringWithSwaps stringWithSwaps = new ReverseStringWithSwaps(); stringWithSwaps.reverseWithSwaps("javaguides"); } public String reverseWithSwaps(String string) { final char[] array = string.toCharArray(); final int length = array.length - 1; final int half = (int) Math.floor(array.length / 2); char c; for (int i = length; i >= half; i--) { c = array[length - i]; array[length - i] = array[i]; array[i] = c; } display(string, String.valueOf(array)); return String.valueOf(array); } private void display(String input, String output) { System.out.println(" input string :: " + input); System.out.println(" output string :: " + output); } }
dengan menukar aksara Output:
input string :: javaguides output string :: sediugavaj
Atas ialah kandungan terperinci Bagaimana untuk membalikkan aksara melalui pertukaran dalam java. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!