From 7a1dc3b25dcd25050ea4693d6c680b902817dec7 Mon Sep 17 00:00:00 2001 From: ktang Date: Thu, 11 Jan 2024 14:48:40 -0800 Subject: [PATCH 1/2] Working on getting my method done --- src/Main.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Main.java b/src/Main.java index 0282a9d..5d0c0fc 100644 --- a/src/Main.java +++ b/src/Main.java @@ -67,6 +67,10 @@ public static void main(String[] args) { } // write your method here + public void add() { + for (int i = 0; i < BUFFER_CAPACITY; i++) { + } + } } \ No newline at end of file From 90568c72a24c5dd1d6a4a9f1bc1a1c4feb2b4ba6 Mon Sep 17 00:00:00 2001 From: ktang Date: Mon, 22 Jan 2024 13:56:17 -0800 Subject: [PATCH 2/2] Working on getting my method done --- src/Main.java | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/src/Main.java b/src/Main.java index 5d0c0fc..0c73cc1 100644 --- a/src/Main.java +++ b/src/Main.java @@ -59,18 +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++; + } + } - // write your method here - public void add() { - for (int i = 0; i < BUFFER_CAPACITY; i++) { + int newIndex = size + spaceCount * 2 - 1; + 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