CC--Q1.3

1.3 URLify: Write a method to replace all spaces in a string with '%20: You may assume that the string has sufficient space at the end to hold the additional characters, and that you are given the "true" length of the string. (Note: If implementing in Java, please use a character array so that you can perform this operation in place.)
EXAMPLE
Input: "Mr John Smith "J 13
Output: "Mr%20J ohn%20Smith"

public String replace(String str) {
    String[] words = str.split(" ");
    StringBuilder sentence = new StringBuilder(words[0]);

    for (int i = 1; i < words.length; ++i) {
        sentence.append("%20");
        sentence.append(words[i]);
    }

    return sentence.toString();
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 13,446评论 0 23
  • 天有些阴冷,何夏裹着着黑色的风衣,脚步匆匆的走向公交站牌。她是打算去找陈晨分手的,本来是可以在微信里说的,但她觉得...
    一只失忆的猫_阅读 2,425评论 0 0
  • 每个人一开始都是一张白纸, 刚开始你可能无法控制在你的这张白纸上, 被绘制什么, 但是慢慢的, 当你自己拥有了橡皮...
    史慧君阅读 4,544评论 13 52