Reverse a sentence

Given a string , " This is a test" reverse it: " tset a si siht" 
Do this recursively.

public class reverseStringRecursive {
    
    
    private static void reverse(char[] s,int start,int end){
        
        if(start > end){
            System.out.println("Reversed String " + String.valueOf(s));
            return;
        }
        if(start == end)
            return;
        
            char temp = s[start];
            s[start] = s[end];
            s[end] = temp;
            reverse(s,start+1,end-1);
            
        
    }
    
    public static void main(String args[]){
        
        String s = "This is a test";
        char []temp = s.toCharArray();
        reverse(temp,0,temp.length-1);
        System.out.println(s);
    }
    
}

 

posted @ 2016-01-23 06:23  Hygeia  阅读(221)  评论(0编辑  收藏  举报