Javaは他の関数を使わず、文字列を所定の規則に従って反転します。

1246 ワード

要求:文字列を与えるとURLに似ています。www.baidu.com   または  wwww.sina.com.cn           与えられた文字列をルールに従って反転させます。comp.baidu.www  /  cn.com.sina.www
 
package     ;

public class TEST {
    /**
     *            :www.baidu.com    www.sina.com.cn
     *               com.baidu.www    cn.com.sina.www
     * @param str
     * @return
     */
    public static String allRotate(String str){
        int strLength = str.length();
        char[] chars = str.toCharArray();
        int left = 0;
        //      
        for (int i = 0; i < strLength; i++){
            if (chars[i] == '.'){
                Rotate(chars, left, i-1);
                left = i + 1;
            }
        }
        Rotate(chars, left, strLength-1);  //        
        Rotate(chars,0,strLength-1);  //     
        return new String(chars);
    }
    public static void Rotate(char[] chars, int start, int end){
        while (start < end){
            char temp = chars[start];
            chars[start] = chars[end];
            chars[end] = temp;
            start++;
            end--;
        }
    }

    public static void main(String[] args) {
        String str = "www.baidu.com";
        System.out.println(allRotate(str));

    }

}