diff --git a/src/Main.java b/src/Main.java index 0282a9d..97c0d94 100644 --- a/src/Main.java +++ b/src/Main.java @@ -60,13 +60,37 @@ public static void main(String[] args) { // call your method here + System.out.println("size: " + changeSpace(buffer, size)); + // check the "after" buffer contents via println // check to see if the new buffer's size is correct - - } // write your method here + public static int changeSpace(char[] charArray, int size) { + // For each index in the charArray + for (int i = 0; i < charArray.length; i++) { + // If the char in index i is an empty space + if (Character.isSpaceChar(charArray[i])) { + // We first would increase the size by 2 since the empty space would be replaced by % and then 2 more for 2 and 0 + size += 2; + + // Starting from the size index which should be the end, we would copy the index -2 which basically means that we would move everything to the right by 2 from the index + for (int j = size; j > i; j--) { + charArray[j] = charArray[j - 2]; + } + + // After the move, we would fill the 3 empty indexes with %, 2, and 0 + charArray[i] = '%'; + charArray[i + 1] = '2'; + charArray[i + 2] = '0'; + + // increase i by 2 since we already know that the next 2 indexes are going to be 2 and 0 + i += 2; + } + } - + System.out.println(Arrays.toString(charArray)); + return size; + } } \ No newline at end of file