Skip to content

Instantly share code, notes, and snippets.

@bitcpf
Created June 19, 2014 20:13
Show Gist options
  • Save bitcpf/95300d061a63c5e32f86 to your computer and use it in GitHub Desktop.
Save bitcpf/95300d061a63c5e32f86 to your computer and use it in GitHub Desktop.
/*
Created by bitcpf, 6/19/2014
*/
public class replaceString {
public static char[] replace(String s){
int s_len = s.length();
int num_of_space = 0;
char[] s_char = s.toCharArray();
// Count the extra space needed for the string
int i = 0, j=0;
while(i < s_len){
if (s_char[i] == ' '){
num_of_space ++;
}
i ++;
}
char[] result = new char[s_len+num_of_space*2];
i = 0;
while(i < s_len){
if (s_char[i] == ' '){
result[j] = '%';
j ++;
result[j] = '2';
j ++;
result[j] = '0';
j ++;
}
else{
result[j] = s_char[i];
j ++;
}
i ++;
}
return result;
}
public static void main(String[] args){
String test = "a b c dadda";
System.out.println(test);
System.out.println(replace(test));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment