Java字符串01-字符串反转输出
字符串反转
package com.clover.demo; import java.util.Arrays; public class Test_StringReverse_0810 { public static void main(String[] args) { // 思路:1.将字符串拆分为单个字符 2.查找中间位置 3.首尾相加=len 4.交换 String line = "abcdef"; char[] chs=line.toCharArray();//转换为字符数组 System.out.println(Arrays.toString(chs)); int a= chs.length;//字符数组的长度 System.out.println(a); int start=0; int end=a-1; if(a%2!=0){ //奇数 //中位数的坐标 int middle=(chs.length-1)/2; //交换 for(int i=0;i<chs.length-1;i++){ if(i<middle){ char tmp = 0; tmp=chs[i]; chs[i]=chs[chs.length-1-i]; chs[chs.length-1-i]= tmp; } } }else{ //偶数 //中间2位数的坐标 int middle= chs.length/2; int middle_1=middle-1; char tmp = 0; tmp=chs[middle]; chs[middle]=chs[middle_1]; chs[middle_1]= tmp; //交换 for(int i=0;i<chs.length/2-1;i++){ if(i<middle_1){ char tmp1 = 0; tmp1=chs[i]; chs[i]=chs[chs.length-1-i]; chs[chs.length-1-i]= tmp1; } } } System.out.println(Arrays.toString(chs)); } }