diff --git a/src/Main.java b/src/Main.java index 0282a9d..0c73cc1 100644 --- a/src/Main.java +++ b/src/Main.java @@ -59,14 +59,41 @@ public static void main(String[] args) { System.out.println("size: " + size); // call your method here - + space(buffer, size); // check the "after" buffer contents via println // check to see if the new buffer's size is correct - + System.out.println(Arrays.toString(buffer)); } + public static void space(char[] buffer, int size) { + int spaceCount = 0; + + // Count the number of spaces in the buffer + for (int i = 0; i < size; i++) { + if (buffer[i] == ' ') { + spaceCount++; + } + } + + int newIndex = size + spaceCount * 2 - 1; - // write your method here + if (newIndex + 1 < buffer.length) { + buffer[newIndex + 1] = '\0'; + } + + // Replace spaces with "%20" + for (int i = size - 1; i >= 0; i--) { + if (buffer[i] == ' ') { + buffer[newIndex] = '0'; + buffer[newIndex - 1] = '2'; + buffer[newIndex - 2] = '%'; + newIndex -= 3; + } else { + buffer[newIndex] = buffer[i]; + newIndex--; + } + } + } } \ No newline at end of file