104 字符串的调整-替换空格
Table of Contents

ac

给定一个字符类型的数组chas[],chas右半区全是空字符,左半区不含有空字符。现在想将左半区的空格字符串替换成“%20”,假设chas右半区足够大,可以满足替换需要的空间,请完成替换函数。
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;

public class Main {
    public static Scanner sc = new Scanner(System.in);

    public static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

    public static void main(String[] args) throws Exception {
        String s = reader.readLine();
        System.out.println(changeBlank(s));
    }

    static char[] changeBlank(String s){
        int len = s.length();
        int space_cnt = 0;
        for (int i = 0; i < len; i++) {
            if (s.charAt(i) == ' ') {
                space_cnt++;
            }
        }
        char[] s2 = new char[len + space_cnt * 2];
        int s2Index = len + space_cnt * 2 - 1;
        for(int i=len-1;i>=0;i--){
            if(s.charAt(i) != ' '){
                s2[s2Index--] = s.charAt(i);
            }else {
                s2[s2Index--] = '0';
                s2[s2Index--] = '2';
                s2[s2Index--] = '%';
            }
        }
        return s2;
    }
}
/*
a  b    c
a%20%20b%20%20%20%20c
 */