From 33102d6ca03bc4e081bb79705412e3ab9d768d3c Mon Sep 17 00:00:00 2001 From: Huy Nguyen Date: Mon, 22 Jan 2024 03:10:47 -0800 Subject: [PATCH 1/3] current solution --- .idea/vcs.xml | 6 ++++++ src/Main.java | 57 +++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 .idea/vcs.xml 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..a3865da 100644 --- a/src/Main.java +++ b/src/Main.java @@ -58,15 +58,68 @@ public static void main(String[] args) { System.out.println(Arrays.toString(buffer)); System.out.println("size: " + size); + + // call your method here // check the "after" buffer contents via println + System.out.println(); // check to see if the new buffer's size is correct - - + System.out.println(addMoreSpace(size,buffer)); } // write your method here + public static char[] addSpaces(String str, char[] buffer){ + int size = 0; + for (int i = 0; i < str.length(); i++) { + char cur = str.charAt(i); + if (cur != ' '){ + buffer[size] = cur; + size++; + } + else { + buffer[size] = '%'; + buffer[size+1] = '2'; + buffer[size+2] = '0'; + size+=3; + } + } + return buffer; + } + public static char[] addMoreSpace(int size, char[] buffer){ + for (int i = size; i > 0; i--) { + char currChar = buffer[i]; + if(currChar == ' '){ + for (int j = i; j < size; j++) { + char curr = buffer[j]; + if (curr != ' '){ + buffer[j] = buffer[j+2]; + } + else { + buffer[j] = '%'; + buffer[j+1] = '2'; + buffer[j+2] = '0'; + size+=3; + } + } + } + } + return buffer; + } + public static char[] shiftRight (int size, int blankStart, char[]buffer){ + for (int i = size; i <= blankStart; i--) { + char curr = buffer[i]; + if (curr != ' '){ + buffer[i] = buffer[i += 2]; + } + else{ + buffer[i] = '%'; + buffer[i+1] = '2'; + buffer[i+2] = '0'; + } + } + return buffer; + } } \ No newline at end of file From 0ef5593170b9999ee3fc7ed4c09431baa46317f2 Mon Sep 17 00:00:00 2001 From: hnben Date: Mon, 22 Jan 2024 04:24:06 -0800 Subject: [PATCH 2/3] Solved the problem with two pointers --- .idea/inspectionProfiles/Project_Default.xml | 298 +++++++++++++++++++ .idea/misc.xml | 2 +- src/Main.java | 77 ++--- 3 files changed, 327 insertions(+), 50 deletions(-) create mode 100644 .idea/inspectionProfiles/Project_Default.xml diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..d40ea0a --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,298 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index 6f29fee..5c02e7d 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/src/Main.java b/src/Main.java index a3865da..da0ca07 100644 --- a/src/Main.java +++ b/src/Main.java @@ -58,66 +58,45 @@ public static void main(String[] args) { System.out.println(Arrays.toString(buffer)); System.out.println("size: " + size); - - // call your method here - + urlify(buffer, size); // check the "after" buffer contents via println - System.out.println(); + System.out.println(Arrays.toString(buffer)); // check to see if the new buffer's size is correct - System.out.println(addMoreSpace(size,buffer)); - } - - // write your method here - public static char[] addSpaces(String str, char[] buffer){ - int size = 0; - for (int i = 0; i < str.length(); i++) { - char cur = str.charAt(i); - if (cur != ' '){ - buffer[size] = cur; + size=0; + for (int i = 0; i < buffer.length; i++) { + //not equals to null + if (buffer[i] != '\u0000'){ size++; } - else { - buffer[size] = '%'; - buffer[size+1] = '2'; - buffer[size+2] = '0'; - size+=3; - } } - return buffer; + System.out.println("Size: " + size); } - public static char[] addMoreSpace(int size, char[] buffer){ - for (int i = size; i > 0; i--) { - char currChar = buffer[i]; - if(currChar == ' '){ - for (int j = i; j < size; j++) { - char curr = buffer[j]; - if (curr != ' '){ - buffer[j] = buffer[j+2]; - } - else { - buffer[j] = '%'; - buffer[j+1] = '2'; - buffer[j+2] = '0'; - size+=3; - } - } + + // write your method here + public static char[] urlify(char[] buffer, int size){ + int spaceSize = 0; + for (int i = 0; i < size; i++) { + if (buffer[i] == ' ') { + spaceSize++; } } - return buffer; - } - public static char[] shiftRight (int size, int blankStart, char[]buffer){ - for (int i = size; i <= blankStart; i--) { - char curr = buffer[i]; - if (curr != ' '){ - buffer[i] = buffer[i += 2]; - } - else{ - buffer[i] = '%'; - buffer[i+1] = '2'; - buffer[i+2] = '0'; + // Calculate the size after adding in %20 in blank spaces + int maxSizePointer = size + spaceSize * 2; + System.out.println("Buffer Size: " + maxSizePointer); + for (int i = size - 1; i >= 0; i--) { + if (buffer[i] == ' ') { + //if there is a blank space, replace with %20 relative to the maxSizePointer at the back + buffer[maxSizePointer - 1] = '0'; + buffer[maxSizePointer - 2] = '2'; + buffer[maxSizePointer - 3] = '%'; + maxSizePointer -= 3; + } else { + //if letter, place within their location relative to maxSizePointer at the back + buffer[maxSizePointer - 1] = buffer[i]; + maxSizePointer--; } } return buffer; From fc71e248338992447146ba193b741d3d6dad82a8 Mon Sep 17 00:00:00 2001 From: hnben Date: Mon, 22 Jan 2024 04:28:10 -0800 Subject: [PATCH 3/3] added javadocs --- src/Main.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/Main.java b/src/Main.java index da0ca07..479ab2b 100644 --- a/src/Main.java +++ b/src/Main.java @@ -35,9 +35,19 @@ with a buffer (similar to how a resizable ArrayList, and/or a import java.util.Arrays; +/** + * The main class to run the main method + * @author Huy Nguyen + * @version 1.0 + */ + public class Main { public static final int BUFFER_CAPACITY = 32768; + /** + * The main method of java + * @param args string arguments of main method + */ public static void main(String[] args) { //TIP Press with your caret at the highlighted text // to see how IntelliJ IDEA suggests fixing it. @@ -75,6 +85,14 @@ public static void main(String[] args) { // write your method here + + /** + * The urlify method to add '%20' at every space and increment the size + * + * @param buffer the char[] which is being manipulated + * @param size current size of the buffer + * @return + */ public static char[] urlify(char[] buffer, int size){ int spaceSize = 0; for (int i = 0; i < size; i++) {