如何实现字符串反转

package java程序员面试笔试宝典;

public class 题8_6_1实现字符串反转 {
	public static void main(String[] args) {
		String str="you are a man who are stupid";
		reverseAll(str);
	}
	public static void reverseAll(String str){
		char[] chs=str.toCharArray();
		reverse(chs, 0, chs.length-1);
		int count=0;
		int begin=0;
		int end=0;
		while(count<chs.length){
			if(chs[count]==' '){
				end=count-1;
				reverse(chs, begin, end);
				begin=end+2;
			}else if(count==chs.length-1){
				end=chs.length-1;
				reverse(chs, begin, end);
			}
			count++;
		}
		str=new String(chs);
		System.out.println(str);
	}
	public static void reverse(char[] a,int begin,int end){
		char temp;
		for (int i = begin,j=end; i < j; i++,j--) {
			temp=a[i];
			a[i]=a[j];
			a[j]=temp;
		}
	}
}

猜你喜欢

转载自blog.csdn.net/m0_38068868/article/details/81737961