解題思路:該題解法很簡單,遍歷字符串,遇到空格,則將空格替換成"%20";java
1 import java.util.Scanner; 2 3 public class Solution { 4 public static void main(String[] args) { 5 Scanner scanner = new Scanner(System.in); 6 StringBuffer str = new StringBuffer(scanner.nextLine()); 7 String result = replaceSpace(str); 8 System.out.println(result); 9 } 10 public static String replaceSpace(StringBuffer str) { 11 for(int i=0 ; i<str.length() ; i++) { 12 if(str.charAt(i) == ' ') { 13 str.replace(i,i+1, "%20"); 14 } 15 } 16 return str.toString(); 17 } 18 }