zoukankan      html  css  js  c++  java
  • 《程序员面试金典》之空格替换

    1、题目描述

      请编写一个方法,将字符串中的空格全部替换为“%20”。假定该字符串有足够的空间存放新增的字符,并且知道字符串的真实长度(小于等于1000),同时保证字符串由大小写的英文字母组成。给定一个string iniString 为原始的串,以及串的长度 int len, 返回替换后的string。

    测试样例:
    "Mr John Smith”,13
    返回:"Mr%20John%20Smith"
    ”Hello  World”,12
    返回:”Hello%20%20World”

    2、代码实现
     1 import java.util.*;
     2 
     3 public class Replacement {
     4     public String replaceSpace(String iniString, int length) {
     5         String newStr = "";
     6         for(int i = 0; i < length; i++){
     7             if(iniString.charAt(i) == ' '){
     8                 newStr += "%20";
     9             }else{
    10                 newStr += iniString.charAt(i);
    11             }
    12         }
    13         return newStr;
    14     }
    15     
    16     public static void main(String[] args) {
    17         Replacement replacement = new Replacement();
    18         System.out.println(replacement.replaceSpace("Hello  World", 12));
    19     }
    20 }
    
    
    
     
  • 相关阅读:
    h5 input调起摄像头、摄像机、录音机
    基数排序
    快速排序 && 希尔排序 && 插入排序
    堆排序
    归并排序(Merge sort)
    动态规划:背包问题
    Chap5: question: 29
    排列 && 组合
    Chap4: question: 19
    Chap3: question: 11
  • 原文地址:https://www.cnblogs.com/wangchaoyuan/p/5965333.html
Copyright © 2011-2022 走看看