From 2251211333a5b93a926863664b30b62c442184f9 Mon Sep 17 00:00:00 2001 From: RJ Trenchard Date: Thu, 11 Jan 2024 13:55:51 -0800 Subject: [PATCH 1/2] solved solution --- src/Main.java | 39 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/src/Main.java b/src/Main.java index 0282a9d..97e3690 100644 --- a/src/Main.java +++ b/src/Main.java @@ -33,6 +33,7 @@ with a buffer (similar to how a resizable ArrayList, and/or a */ +import java.nio.BufferOverflowException; import java.util.Arrays; public class Main { @@ -48,7 +49,7 @@ public static void main(String[] args) { int size = 0; // initialize the buffer and size variables with some data - String temp = "Dr Martin Luther King"; + String temp = " Dr Martin Luther King "; for (int i = 0; i < temp.length(); i++) { buffer[i] = temp.charAt(i); } @@ -59,14 +60,48 @@ public static void main(String[] args) { System.out.println("size: " + size); // call your method here + size = modifyCStr(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)); + // check to see if the new buffer's size is correct + System.out.println("size: " + size); } // write your method here + /** + * + * @param buf char string + * @param size the size of the string + * @return the new size of the string + */ + public static int modifyCStr(char[] buf, int size) { + for (int i = size; i >= 0; i--) { + switch(buf[i]) { + case ' ': + // increase string size + size += 2; + + // do not try if too big + if (size >= BUFFER_CAPACITY) throw new BufferOverflowException(); + + // shift all to right of index right by 2 + for (int j = size -1; j >= i + 2 ; j--) { + buf[j] = buf[j-2]; + } + + // set the character to the new values + buf[i] = '%'; + buf[i+1] = '2'; + buf[i+2] = '0'; + + break; + } + } + return size; + } } \ No newline at end of file From 77a60b12f01fba841d38939042cab74fd87805c3 Mon Sep 17 00:00:00 2001 From: RJ Trenchard Date: Thu, 11 Jan 2024 13:58:33 -0800 Subject: [PATCH 2/2] added modifyCStr --- src/Main.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/Main.java b/src/Main.java index 97e3690..d4a5a79 100644 --- a/src/Main.java +++ b/src/Main.java @@ -90,14 +90,20 @@ public static int modifyCStr(char[] buf, int size) { // shift all to right of index right by 2 for (int j = size -1; j >= i + 2 ; j--) { + // a,_,b,c,\0,\0 + // will become + // a,_,b,c,b,c buf[j] = buf[j-2]; } // set the character to the new values + // array will become + // a,%,2,0,b,c buf[i] = '%'; buf[i+1] = '2'; buf[i+2] = '0'; + break; } }