From 6e38ef1e0552389ee3d0c6ccce7d9f7c57b11e28 Mon Sep 17 00:00:00 2001 From: braedonbillingsley Date: Tue, 6 Feb 2024 20:54:56 -0800 Subject: [PATCH] Revised code and fixed replaceSpaces method. Removed unused imports and fields. --- src/Main.java | 42 +++++++++++++++++++++++++++++++++--------- 1 file changed, 33 insertions(+), 9 deletions(-) diff --git a/src/Main.java b/src/Main.java index 0282a9d..88358ba 100644 --- a/src/Main.java +++ b/src/Main.java @@ -32,12 +32,15 @@ with a buffer (similar to how a resizable ArrayList, and/or a CareerCup, Palo Alto, CA. */ - import java.util.Arrays; +/** + * SDEV 333 assignment URLify, practice interview question. + * + * @author Braedon Billingsley + */ public class Main { public static final int BUFFER_CAPACITY = 32768; - public static void main(String[] args) { //TIP Press with your caret at the highlighted text // to see how IntelliJ IDEA suggests fixing it. @@ -45,10 +48,11 @@ public static void main(String[] args) { // set up empty buffer, size of 0 char[] buffer = new char[BUFFER_CAPACITY]; - int size = 0; + int size; // initialize the buffer and size variables with some data - String temp = "Dr Martin Luther King"; + //String temp = "Dr Martin Luther King"; + String temp = "Hello, World!"; for (int i = 0; i < temp.length(); i++) { buffer[i] = temp.charAt(i); } @@ -59,14 +63,34 @@ 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)); + // check to see if the new buffer's size is correct + System.out.println("new buffer size: " + size); } - // write your method here - - + // Replaces spaces in array of char buffer with '%20' + public static int replaceSpaces(char[] buffer, int size) { + // Iterate the size of the buffer + for(int i = 0; i < size; i++) { + + // Check if index is equal to a ' ' + if (buffer [i] == ' ' ) { + // Shift over list at each space by 2; + for (int j = size + 3; j >= i; j--) { + buffer[j + 2] = buffer[j]; + } + + // Add %20 to spaces + buffer[i] = '%'; + buffer[i + 1] = '2'; + buffer[i + 2] = '0'; + size = size + 2; + } + } + return size; + } } \ No newline at end of file