Skip to content

Commit a599677

Browse files
author
Geektimus
committed
Added a challenge to make a string URL Friendly
1 parent 3e39726 commit a599677

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ We need to write a function that will tell us if all the characters on a given s
6565
### Check permutation
6666
We need to write a function that given two strings let us find if one is a permutation of the other.
6767

68+
### URL Friendly String
69+
Given a String and its true length, transform the string to a URL friendly string by replacing the spaces by %20
70+
71+
**Note:** We can assume that the string contains a length that allow us to store the additional chars
72+
without having to allocate a new string.
73+
6874
### Others (Challenges proposed by Friends or Solved on hackatons)
6975
#### Recursively reverse a string
7076
Given a string, reverse the characters and return

src/main/java/com/codingmaniacs/others/StringChallenges.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,44 @@ static String findFirstRepeatedChar(String str) {
3939
}
4040
return "";
4141
}
42+
43+
/**
44+
* Given a String and its true length, transform the string to a URL friendly string by replacing the spaces by %20
45+
*
46+
* @param str String to be transformed
47+
* @param trueLength True size of the string without the trailing spaces.
48+
* @return A URL friendly string with the spaces replaced.
49+
*/
50+
static String URLlify(String str, int trueLength) {
51+
if (str.length() == trueLength)
52+
return str;
53+
54+
char[] chars = str.toCharArray();
55+
int spaces = 0;
56+
57+
// Count spaces
58+
for (int i = 0; i < trueLength; i++) {
59+
if (chars[i] == ' ') {
60+
spaces++;
61+
}
62+
}
63+
64+
if (trueLength < str.length()) chars[trueLength] = '\0';
65+
66+
// Replace the string backwards so we don't need to worry about overriding data.
67+
int index = trueLength + spaces * 2;
68+
for (int j = trueLength - 1; j >= 0; j--) {
69+
if (chars[j] == ' ') {
70+
chars[index - 1] = '0';
71+
chars[index - 2] = '2';
72+
chars[index - 3] = '%';
73+
index = index - 3;
74+
} else {
75+
chars[index - 1] = chars[j];
76+
index--;
77+
}
78+
}
79+
80+
return new String(chars);
81+
}
4282
}

src/test/java/com/codingmaniacs/others/StringChallengesTests.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,34 @@ public void findFirstOccurrenceOfCharLongerString() {
9595
String result = StringChallenges.findFirstRepeatedChar(str);
9696
assertEquals("Since the letter a is repeated once, that's the solution", expected, result);
9797
}
98+
99+
@Test
100+
public void transformStringToURLNoSpaces() {
101+
String str = "helloworld";
102+
int trueLength = 10;
103+
String expected = "helloworld";
104+
105+
String result = StringChallenges.URLlify(str, trueLength);
106+
assertEquals("We don't have spaces to parse so we expect the same string", expected, result);
107+
}
108+
109+
@Test
110+
public void transformStringToURLTwoSpaces() {
111+
String str = "hello world! example ";
112+
int trueLength = 20;
113+
String expected = "hello%20world!%20example";
114+
115+
String result = StringChallenges.URLlify(str, trueLength);
116+
assertEquals(expected, result);
117+
}
118+
119+
@Test
120+
public void transformStringToURLFourSpaces() {
121+
String str = "this is my friendly command ";
122+
int trueLength = 27;
123+
String expected = "this%20is%20my%20friendly%20command";
124+
125+
String result = StringChallenges.URLlify(str, trueLength);
126+
assertEquals(expected, result);
127+
}
98128
}

0 commit comments

Comments
 (0)