通过交换字符反转
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中文网其他相关文章!