package com.thinker.test.test1;

/**
 * 编写一个方法，将字符串中的全部替换为"%20"，假定该字符串尾部有足够空间存放新增字符
 * 并且知道字符串的真实长度。
 */
public class Test_1_4_1 {

    private static char[] replace(char[] str){
        int length = str.length,space = 0,newLength;

        for(int i=0;i<length;i++){
            if(str[i] == ' ') space ++;
        }
        newLength = length + space * 2;
        char[] tmp = new char[newLength];
        for(int i=length - 1;i>=0;i--){
            if(str[i] == ' '){
                tmp[newLength-1] = '0';
                tmp[newLength-2] = '2';
                tmp[newLength-3] = '%';
                newLength -= 3;
            }else {
                tmp[newLength - 1] = str[i];
                newLength -= 1;
            }
        }
        return tmp;
    }

    public static void main(String[] args) {
        System.out.println(replace("hello world".toCharArray()));
    }
}
