码迷,mamicode.com
首页 > 其他好文 > 详细

1.4---字符串空格变成20%(CC150)

时间:2015-12-16 18:52:52      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:

import CtCILibrary.AssortedMethods;

public class Question {
    // Assume string has sufficient free space at the end
    public static void replaceSpaces(char[] str, int length) {
        int spaceCount = 0, index, i = 0;
        for (i = 0; i < length; i++) {
            if (str[i] == ‘ ‘) {
                spaceCount++;
            }
        }
        index = length + spaceCount * 2;
        str[index] = ‘\0‘;
        for (i = length - 1; i >= 0; i--) {
            if (str[i] == ‘ ‘) {
                str[index - 1] = ‘0‘;
                str[index - 2] = ‘2‘;
                str[index - 3] = ‘%‘;
                index = index - 3;
            } else {
                str[index - 1] = str[i];
                index--;
            }
        }
    }
    
    public static void main(String[] args) {
        String str = "abc d e f";
        char[] arr = new char[str.length() + 3 * 2 + 1];
        for (int i = 0; i < str.length(); i++) {
            arr[i] = str.charAt(i);
        }
        replaceSpaces(arr, str.length());    
        System.out.println("\"" + AssortedMethods.charArrayToString(arr) + "\"");
    }
}

 

1.4---字符串空格变成20%(CC150)

标签:

原文地址:http://www.cnblogs.com/yueyebigdata/p/5051805.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!