class MyReverse{
public static String myreverse(String s){
char[] c=s.toCharArray();//将字符串转换成字符数组
char temp;
int n=s.length();
//字符数组首尾依次交换
for(int i=0,j=n-1;i<n/2;i++,j--){
temp=c[i];
c[i]=c[j];
c[j]=temp;
}
//将反转后的字符数组转换成字符串返回
return new String(c);
}
public static void main(String[] args){
String s="abcdef";
System.out.println(s);
System.out.println("反转后:"+myreverse(s));
}
} |
|