package com.thinker.test.test1;
/**
 * 编写一个方法，将字符串中的全部替换为"%20"，假定该字符串尾部有足够空间存放新增字符
 * 并且知道字符串的真实长度。
 */
public class Test_1_4{

	private static void doSomething(){
		String str = "Mr John Smith";
		int num = 0;
		char[] c = new char[str.length()];
		System.out.print("origin str:"+str+"\n");
		for(int i=0;i<str.length();i++){
			if(str.charAt(i)==' '){
				num += 1;
			}
//			System.out.print(str.charAt(i));
			c[i] = str.charAt(i);
		}
		int count = 0;
		int len = c.length+(num*2);
		char[] result = new char[len];
		for(int i=0;i<c.length;i++){
			if(c[c.length-i-1] == ' '){
				result[len-i-1-2*count] = '0';
				result[len-i-2-2*count] = '2';
				result[len-i-3-2*count] = '%';
				count += 1;
			}else{
				result[len-i-2*count-1] = c[c.length-i-1];
			}
		}
		System.out.print("finish:");
		for(int i=0;i<result.length;i++){
			System.out.print(result[i]);
		}
		System.out.print("\n");
	}
	public static void main(String[] args){
		doSomething();
	}
}
