package com.jenthor;
public class StingTest {
/*
* 练习二:将字符串反转
*/
public static void main(String[] args) {
String s = "abcdef";
char[] chs = s.toCharArray();
printArray(chs);
reverse(chs);
printArray(chs);
}
public static void reverse(char arr[]) {
for (int start = 0, end = arr.length - 1; start < end; start++, end--) {
swap(arr, start, end);
}
}
private static void swap(char[] arr, int x, int y) {
char temp = arr[x];
arr[x] = arr[y];
arr[y] = temp;
}
public static void printArray(char arr[]) {
System.out.print("[");
for (int i = 0; i < arr.length; i++) {
if (i != arr.length - 1)
System.out.print(arr[i] + ",");
else
System.out.println(arr[i] + "]");
}
}
}