package com.thinker.bishi.offer;
public class ReplaceSpace{


	public static String replaceSpace(StringBuffer str){
		if(str == null) return null;
		StringBuilder tmp = new StringBuilder();
		for(int i = 0;i<str.length();i++){
			if(str.charAt(i) == ' ') tmp.append("%").append("2").append("0");
			else tmp.append(str.charAt(i));
		}
		return tmp.toString();
	}

	public static String go(StringBuffer str){
		if(str == null) return null;
		int space = 0;
		for(int i = 0;i < str.length();i++){
			if(str.charAt(i) == ' ') space ++;
		}
		int newLength = str.length() + space * 2;
		char[] chars = new char[newLength];
		int j = newLength - 1;
		for(int i = str.length() - 1;i>= 0;i--){
			if(str.charAt(i) == ' ') {
				chars[j] = '0';chars[j-1] = '2';chars[j-2] = '%'; j -= 3;
			}else{
				chars[j] = str.charAt(i); j--;
			}
		}

		return new String(chars);
	
	}


	public static void main(String[] args){
		StringBuffer str = new StringBuffer("We Are Happy");
		System.out.println(go(str));
	}
}
