逆転文字列Write a String Reverser(and use Recursion!)

1490 ワード

public class Reverse {

	public static String ReverseStr(String str){
		
		if(str==null&&str.length()<=1){
			return str;
		}else{
			return new StringBuffer(str).reverse().toString();
		}
		
	}
	
	public static String ReverseStr1(String str){
		 if ((null == str) || (str.length()  <= 1)) {
			 return str;
         }
		 return ReverseStr1(str.substring(1)) + str.charAt(0);
	}
	
	public static String ReverseStr2(String str){
		 if ((null == str) || (str.length()  <= 1)) {
             return str;
         }else{
        	 String temp = "";
        	 char charArray[]=str.toCharArray();
        	 for(int i=charArray.length-1;i>=0;i-- ){
        		 temp += charArray[i];
        	 }
        	 return temp;
         }
         
	}
	
	
	
	public static void main(String[] args) {
		System.out.println(ReverseStr("oywl"));
		System.out.println(ReverseStr1("oywl"));
		System.out.println(ReverseStr2("oywl"));
	}

}

 
 
 
http://codemonkeyism.com/java-interview-questions-write-a-string-reverser-and-use-recursion/