本帖最后由 luqh 于 2015-3-24 20:36 编辑
- public class Test1 {
- public static void main(String[] args) {
- String s = "abcd";
- s = reverseStr(s);
- sop(s);
- }
- public static void sop(String str) {
- System.out.println(str);
- }
- // 将字符串反转
- public static String reverseStr(String str) {
- char[] cha = str.toCharArray();// 变数组
- reverse(cha);
- return new String(cha);
- }
- public static void reverse(char[] arr) {
- for (int start = 0, end = arr.length - 1; start < end; start++) {
- swp(arr, start, end);
- }
- }
- public static void swp(char[] arr, int x, int y) {
- char tmp = arr[x];
- arr[x] = arr[y];
- arr[y] = tmp;
- }
- }
复制代码 |
|