package swordOffre.gaopengyu;
public class StringReplace5 {
public static void main(String[] args) {
String s = "we are happy.";
String result = getReplaceString(s);
System.out.println(result);
}
public static String getReplaceString(String s) {
char[] sChar = s.toCharArray();
//空格的总数
int space = 0;
for (int i = 0; i < sChar.length; i++) {
if (sChar[i] == ' ') {
space++;
}
}
char[] sCharReplaced = new char[sChar.length + 2 * space];
int i = sChar.length - 1;
int j = sCharReplaced.length - 1;
while (i >= 0) {
if (sChar[i] != ' ') {
sCharReplaced[j] = sChar[i];
j--;
} else {
sCharReplaced[j] = '0';
sCharReplaced[j - 1] = '2';
sCharReplaced[j - 2] = '%';
j = j - 3;
}
i--;
}
return String.valueOf(sCharReplaced);
}
}
11-22
08-29
08-29
08-29