patternjavaMinor
Write a method to replace all spaces in a string with %20
Viewed 0 times
methodallwithreplacewritespacesstring
Problem
I have implemented a solution to this Cracking the Coding Interview question:
Write a method to replace all spaces in a string with %20.
You may assume that the string has sufficient space at the end of the string to hold the additional characters,
and that you are given the true length of the string.
USE character array to perform this operation in place.
Can anyone give me any feedback on my implementation in regards of efficiency or improvements?
Write a method to replace all spaces in a string with %20.
You may assume that the string has sufficient space at the end of the string to hold the additional characters,
and that you are given the true length of the string.
USE character array to perform this operation in place.
public static String conversion(String str, int length){
char[] strChars = str.toCharArray();
int numSpaces = 0;
for(int i = 0; i = 0; i--){
char c = strChars[i];
if(c != ' '){
newChars[i + 2 * numSpaces] = c;
}
else{
newChars[i + 2 * numSpaces] = '0';
newChars[i + 2 * numSpaces - 1] = '2';
newChars[i + 2 * numSpaces - 2] = '%';
numSpaces--;
}
}
String newString = String.valueOf(newChars);
return newString;
}Can anyone give me any feedback on my implementation in regards of efficiency or improvements?
Solution
Your strategy of counting the spaces and then back-looping to shift the characters right (and replace spaces with
Your variable names are decent, and the code flows well.
On the other hand, there are some small things I would change.
Possible bug
Your code, given the input
In fact, you should only really have the one
Enhanced fors
Use enhanced-for loops when possible, and always use
should be:
Comments
Comment unusual loops - your second for-loop is an odd one, and it often helps the reader if you comment why a loop is non-sequential (or even if you just make sure they are aware of it).
multiple indexes
Have you considered having a separate index for each position in the array - the position of the source character, and the position of where to insert it?
The benefit of two indexes is that you can keep the logic more readable ... ("more" is relative) as each index moves by one character at a time... and a space counts as 3 characters.
%20) is good. The basic algorithm is probably as good as it gets as a character array system.Your variable names are decent, and the code flows well.
On the other hand, there are some small things I would change.
Possible bug
Your code, given the input
conversion("abc ", 3) you would output "abc" but you should not remove any "extra" padding in the string, you should return "abc "In fact, you should only really have the one
char[] array. The second one is making you do bad things ;-)Enhanced fors
Use enhanced-for loops when possible, and always use
{} blocks for if-statements, even 1-liners:for(int i = 0; i < length; i++){
if(strChars[i] == ' ')
numSpaces++;
}should be:
for (char c : strChars) {
if (c == ' ') {
numSpaces++;
}
}Comments
Comment unusual loops - your second for-loop is an odd one, and it often helps the reader if you comment why a loop is non-sequential (or even if you just make sure they are aware of it).
multiple indexes
Have you considered having a separate index for each position in the array - the position of the source character, and the position of where to insert it?
public static String conversion(String str, int length){
char[] strChars = str.toCharArray();
int numSpaces = 0;
for (int i = 0; i = 0; i--) {
char c = strChars[i];
if (c == ' '){
strChars[insert--] = '0';
strChars[insert--] = '2';
strChars[insert--] = '%';
} else {
strChars[insert--] = c;
}
}
return String.valueOf(strChars);
}The benefit of two indexes is that you can keep the logic more readable ... ("more" is relative) as each index moves by one character at a time... and a space counts as 3 characters.
Code Snippets
for(int i = 0; i < length; i++){
if(strChars[i] == ' ')
numSpaces++;
}for (char c : strChars) {
if (c == ' ') {
numSpaces++;
}
}public static String conversion(String str, int length){
char[] strChars = str.toCharArray();
int numSpaces = 0;
for (int i = 0; i < length; i++) {
if(strChars[i] == ' ') {
numSpaces++;
}
}
int insert = length + 2 * numSpaces - 1;
// loop backwards through source characters.
for (int i = length - 1; i >= 0; i--) {
char c = strChars[i];
if (c == ' '){
strChars[insert--] = '0';
strChars[insert--] = '2';
strChars[insert--] = '%';
} else {
strChars[insert--] = c;
}
}
return String.valueOf(strChars);
}Context
StackExchange Code Review Q#156821, answer score: 5
Revisions (0)
No revisions yet.