From 669629bbe7625814f89b0dd34e5e4a70989455f8 Mon Sep 17 00:00:00 2001 From: ryderdettloff Date: Sun, 21 Jan 2024 22:49:33 -0800 Subject: [PATCH] fixed the counter method. Need to update the name of the method as it may cause confusion. But the Method correctly counts the arrays size, doubles it, then uses a for loop to replace the white spaces with %,2, then 0. If no white spaces are present, it copies from the original array. --- .idea/misc.xml | 1 - .idea/vcs.xml | 6 ++++++ src/LinkedList.java | 1 + src/Main.java | 33 ++++++++++++++++++++++++++++----- 4 files changed, 35 insertions(+), 6 deletions(-) create mode 100644 .idea/vcs.xml create mode 100644 src/LinkedList.java 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/LinkedList.java b/src/LinkedList.java new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/src/LinkedList.java @@ -0,0 +1 @@ + diff --git a/src/Main.java b/src/Main.java index 0282a9d..5229188 100644 --- a/src/Main.java +++ b/src/Main.java @@ -57,16 +57,39 @@ public static void main(String[] args) { // check the "before" buffer and size via println System.out.println(Arrays.toString(buffer)); System.out.println("size: " + size); - // call your method here - + counter(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); } - // write your method here - + public static void counter(char[] buffer, int size) { //changed from int to void to avoid return statement + int spaceNumbers = 0; + for (int j = 0; j < size; j++) { + if (buffer[j] == ' ') { + spaceNumbers++; + // Start by counting the spaces in the array + } //fixed an issue where using an int as a variable required a return statement. + // it creates confusion and is easiest set as void + } + int newArray = size + (spaceNumbers * 2); //double each space to create space for the %20 + //This for loop goes through each white space and inserts a % then 2 then 0. + // note the array SIZE doubles so that the new buffer has enough space + //This is different that the length of the array in which there seams to be plenty of room. + for (int i = size - 1; i >= 0; i--) { + if (buffer[i] == ' ') { + buffer[newArray - 1] = '0'; + buffer[newArray - 2] = '2'; + buffer[newArray - 3] = '%'; + newArray = newArray - 3; + } else { + buffer[newArray - 1] = buffer[i]; //copy the original buffer to the new array + newArray--; + } + } + } } \ No newline at end of file