From 965ef3555377e221e020ee4b8ddf6364e749a785 Mon Sep 17 00:00:00 2001 From: Daniel Date: Thu, 11 Jan 2024 13:55:58 -0800 Subject: [PATCH 1/2] added replaceSpaces(char[] buffer, int size) function to Main.java. --- src/Main.java | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/Main.java b/src/Main.java index 0282a9d..882c52e 100644 --- a/src/Main.java +++ b/src/Main.java @@ -38,6 +38,23 @@ with a buffer (similar to how a resizable ArrayList, and/or a public class Main { public static final int BUFFER_CAPACITY = 32768; + /** + * this function will convert any space characters with '%' '2' '0' + */ + public static int replaceSpaces(char[] buffer, int size) { + for (int i = 0; i < size; i++) { + if (buffer[i] == ' ') { + for (int j = size; j > i; j--) { + buffer[j] = buffer[j - 2]; + } + buffer[i] = '%'; + buffer[i + 1] = '2'; + buffer[i + 2] = '0'; + size = size + 2; + } + } + return size; + } public static void main(String[] args) { //TIP Press with your caret at the highlighted text // to see how IntelliJ IDEA suggests fixing it. @@ -59,9 +76,11 @@ public static void main(String[] args) { System.out.println("size: " + size); // call your method here - + size = replaceSpaces(buffer, size); // check the "after" buffer contents via println + System.out.println(Arrays.toString(buffer)); // check to see if the new buffer's size is correct + System.out.println("size: " + size); } From 5186b9a1494a8fe64f2e7f803ca8c4432ef327b5 Mon Sep 17 00:00:00 2001 From: Daniel Date: Mon, 22 Jan 2024 15:28:24 -0800 Subject: [PATCH 2/2] modified inner for loop to fully shift the string after each space is replaced with %20 --- src/Main.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Main.java b/src/Main.java index 882c52e..7980720 100644 --- a/src/Main.java +++ b/src/Main.java @@ -44,7 +44,7 @@ public class Main { public static int replaceSpaces(char[] buffer, int size) { for (int i = 0; i < size; i++) { if (buffer[i] == ' ') { - for (int j = size; j > i; j--) { + for (int j = size + 1; j > i; j--) { buffer[j] = buffer[j - 2]; } buffer[i] = '%';