public static String replaceSpace(String input){
char[] inputs = input.toCharArray();
char[] containers = new char[100];
int originLength = inputs.length;
int spaceLength = 0;
int i = 0;
while(i < originLength) {
if(' ' == inputs[i]) {
spaceLength ++;
}
containers[i] = inputs[i];
i ++;
}
int changedLength = originLength + spaceLength * 2;
int outputLength = originLength + spaceLength * 2;
while(originLength > 0 && originLength < changedLength) {
if(containers[originLength - 1] == ' '){
containers[-- changedLength] = '+';
containers[-- changedLength] = '+';
containers[-- changedLength] = '+';
} else {
containers[--changedLength] = containers[originLength - 1];
}
originLength --;
}
String result = "";
int j = 0;
while (outputLength > 0) {
String index = String.valueOf(containers[j]);
result = result.concat(index);
outputLength --;
j++;
}
return result;
}
网友评论