Skip to content

Instantly share code, notes, and snippets.

@jyhjuzi
Last active August 29, 2015 14:02
Show Gist options
  • Save jyhjuzi/dc69612f9ef6be06b224 to your computer and use it in GitHub Desktop.
Save jyhjuzi/dc69612f9ef6be06b224 to your computer and use it in GitHub Desktop.
public class Q1_4 {
public static void main(String args[]){
char[] test = {'a',' ','b',' ',' ','c',' ',' ',' ',' ',' ',' '};
System.out.println(process(test,6));
}
static String process(char[] array, int length){
int numberOfSpace = 0;
for(int i = 0; i<length; i++){
if(array[i] == ' ')
numberOfSpace++;
}
int retLength = length + 2*numberOfSpace;
int j = length-1;
for(int i = retLength-1; i>=0; ){
if(array[j] == ' '){
array[i]='0';
array[i-1]='2';
array[i-2] ='%';
i = i-3;
}
else{
array[i] = array[j];
i--;
}
j--;
}
return new String(array);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment