import java.util.Scanner;
public class Solution
{
public static String replaceSpace(String s)
{
StringBuilder result = new StringBuilder();
char[] array = s.toCharArray();
for (int x = 0; x < array.length; x++) {
if (array[x] == ' ') {
result.append("%20");
System.out.println(result.toString());
} else {
result.append(array[x]);
System.out.println(result.toString());
}
}
return result.toString();
}
public static void main(String[] args)
{
Scanner data_in=new Scanner(System.in);
System.out.println(replaceSpace(data_in.next()));
}
}
以上是代码,作用是把输入的字符串中的空格替换为%20,但是每次测试到空格就会直接停下循环。
如输入
everybody want to
会输出
everybody