static void replaceSpaces(char[] str, int length) {
        // 1st scan: count spaces
        int cnt = 0;
        for (char ch : str) {
            if (ch == ' ') ++cnt;
        }

        // 2nd scan: replace spaces
        int p = length + 2 * cnt;
        str[p] = '\0'; //XXX
        --p;
        for (int i = length - 1; i >= 0; --i) {
            if (str[i] == ' ') {
                str[p--] = '0';
                str[p--] = '2';
                str[p--] = '%';
            } else {
                str[p--] = str[i];
            }
        }
    }