From d95f4e309a534aa7f6076691a0d78c6ac7e0dcd1 Mon Sep 17 00:00:00 2001 From: Gecervantes01 Date: Thu, 11 Jan 2024 14:02:34 -0800 Subject: [PATCH 1/2] Wrote 2 methods to solve the problem. replaceSpaces() & shiftRight(). --- .idea/misc.xml | 1 - .idea/vcs.xml | 6 ++++++ src/Main.java | 30 +++++++++++++++++++++++++++++- 3 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 .idea/vcs.xml diff --git a/.idea/misc.xml b/.idea/misc.xml index 6f29fee..5af9c98 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,3 @@ - diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/Main.java b/src/Main.java index 0282a9d..eb79ef5 100644 --- a/src/Main.java +++ b/src/Main.java @@ -59,14 +59,42 @@ 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 // check to see if the new buffer's size is correct - + System.out.println(Arrays.toString(buffer)); + System.out.println("size: " + size); } // write your method here + public static int replaceSpaces(char[] buffer, int size) { + // loops through buffer array + for(int i = 0; i < size; i++) { + // checks if char is equal to whitespace + if(buffer[i] == ' ') { + + // loops from end of the array up until the whitespace + shiftRight(buffer, i, size); + + // adds all the new characters + buffer[i] = '%'; + buffer[i + 1] = '2'; + buffer[i + 2] = '0'; + + // increases size + size += 2; + } + } + return size; + } + + public static void shiftRight(char[] buffer, int start, int size) { + for(int i = size + 2; i > start; i--) { + buffer[i] = buffer[i - 2]; + } + } } \ No newline at end of file From 357117b0c6d8ab8709c8d7e3708c767622fa8a1a Mon Sep 17 00:00:00 2001 From: Gecervantes01 Date: Tue, 16 Jan 2024 19:06:24 -0800 Subject: [PATCH 2/2] Made a fix with the shifRight() method where a space at index 0 or 1 would throw an indexOutOfBounds exception --- src/Main.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Main.java b/src/Main.java index eb79ef5..c7f9bb2 100644 --- a/src/Main.java +++ b/src/Main.java @@ -92,7 +92,13 @@ public static int replaceSpaces(char[] buffer, int size) { public static void shiftRight(char[] buffer, int start, int size) { for(int i = size + 2; i > start; i--) { - buffer[i] = buffer[i - 2]; + // in case the space is at the index 0 or 1 + if(start <= 1 && i == (start + 1)) { + buffer[i] = '*'; + } else { + buffer[i] = buffer[i - 2]; + } + } }