Reverse String

package org.jyjiao;

public class Reverse{
	public String reverse(String str){
		char[] array=str.toCharArray();
		int len=array.length;
		char tmp;
		int i=0,j=len-1;
		while(i<j){
			tmp=array[i];
			array[i]=array[j];
			array[j]=tmp;
			i++;
			j--;
			
		}
	
		int index=0,start=0,end=0;
		while(index<len){
			while(index<len && array[index]!=' '){
				index++;
			}
			end=index-1;
			while(start<end){
				tmp=array[start];
				array[start]=array[end];
				array[end]=tmp;
				start++;
				end--;
			}
			start=index+1;
			index++;
		}

		str=new String(array);
		return str;
	}
	
	
	public static void main(String[] args){
		Reverse r=new Reverse();
		String str=r.reverse("I love you baby");
		System.out.println(str);
	}
}
		
	

 

你可能感兴趣的:(J#)