Cracking the coding interview--Q1.5
2014-10-20 16:09
351 查看
原文:
Write a method to replace all spaces in a string with ‘%20’.
译文:
写一个函数,把字符串中所有的空格替换为%20 。
Write a method to replace all spaces in a string with ‘%20’.
译文:
写一个函数,把字符串中所有的空格替换为%20 。
package chapter_1_arraysandstring; import java.util.Scanner; /** * 写一个函数,把字符串中所有的空格替换为%20 * * @author LiangGe * */ public class Question_1_5 { /** * 通过StringBuffer拼接结果,遍历替换 */ public static String spaceReplace(String str) { StringBuffer result = new StringBuffer(); for (int i = 0; i < str.length(); i++) { if (str.charAt(i) == ' ') { result.append("%20"); } else { result.append(str.charAt(i)); } } return result.toString(); } public static void main(String args[]) { Scanner scanner = new Scanner(System.in); while (scanner.hasNextLine()) { String str = scanner.nextLine(); str = spaceReplace(str); System.out.println(str); } } }
相关文章推荐
- Cracking the coding interview--Q1.5
- Cracking the coding interview--Q1.5
- Cracking the coding interview--Q1.5
- Cracking the coding interview--Q1.5
- 【Cracking the coding interview】Q1.5(替换字符串)
- Cracking the coding interview--Q2.3
- Cracking the coding interview--Q1.1
- 《Cracking the Coding Interview》读书笔记
- Cracking the coding interview--Q12.1
- 《Cracking the Coding Interview》——第2章:链表——题目1
- Cracking the Coding Interview Q1.8
- Cracking the coding interview--Q12.6
- 《Cracking the Coding Interview》——第5章:位操作——题目1
- 《Cracking the Coding Interview》——第7章:数学和概率论——题目6
- Cracking the coding interview--14.1-14.6
- Cracking The Coding Interview 1.2
- Cracking The Coding Interview 1.5
- Cracking the coding interview--Q17.4
- cracking the coding interview 中文版
- Cracking the coding interview--Q1.4