-
Notifications
You must be signed in to change notification settings - Fork 0
/
URLify.java
44 lines (37 loc) · 855 Bytes
/
URLify.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// CCI pg.90, #1.3 Check Permutations
//replace all non-contiguous spaces in a string with '%20'
import java.util.*;
class URLify {
public static void main(String[] args) {
String s = "Mr Joe Smith ";
System.out.println(URLify(s, 12));
}
public static char[] URLify(String s, int length) {
char[] c = s.toCharArray();
if (!(s.contains(" "))) {return c;}
System.out.println(c);
int numSpaces = 0;
for(int i =0; i <s.length(); i++) {
if(c[i] == ' ') {
numSpaces++;
}
}
System.out.println(numSpaces);
int fullLength = s.length();
for(int i = length-1; i >=0; i--) {
if(c[i] != ' ') {
c[fullLength-1] = c[i];
fullLength--;
}
else {
c[fullLength-1] = '0';
c[fullLength-2] = '2';
c[fullLength-3] = '%';
fullLength--;
fullLength--;
fullLength--;
}
}
return c;
}
}