透過交換字元反轉
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); } }
輸出:
input string :: javaguides output string :: sediugavaj
以上是java如何透過交換實現字元反轉的詳細內容。更多資訊請關注PHP中文網其他相關文章!