1.怎样实现字符串的反转比如:“how are you”--->“you are how”
public class InverseString { public void swap(char[] arr,int front,int end){//将char[]反转 while(front<end){ char temp=arr[end]; arr[end]=arr[front]; arr[front]=temp; front++; end--; } } public String swapWords(String s){ char[] arr=s.toCharArray();//将字符串转化为char类型数组 swap(arr, 0, arr.length-1);//先将char[]反转 int begin=0; for(int i=1;i<arr.length;i++){//仅仅要遇到空格就将空格之前的单词反转 if("".equals(arr[i])){ swap(arr, begin, i-1); begin=i+1; } } swap(arr, begin, arr.length-1);//由于最后一位没有空格所以最后一个单词手动反转 return new String(arr); } public static void main( String[] args){ String s="how are you"; System.out.println(new InverseString().swapWords(s)); } }