Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 29 additions & 2 deletions src/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,41 @@ public static void main(String[] args) {
System.out.println("size: " + size);

// call your method here
replaceSpaces(buffer, size);

// check the "after" buffer contents via println
// check to see if the new buffer's size is correct

// check to see if the new buffer's size is correct

}

// write your method here
public static void replaceSpaces(char[] buffer, int size){
//count the number of spaces
int spaceCount = 0;
for (int i = 0; i < size; i++) {
if (buffer[i] == ' ') {
spaceCount++;
}
}

int newSize = size + (spaceCount * 2);

int index = newSize - 1;

//start at the end
for (int i = size - 1; i >= 0; i--) {
if (buffer[i] == ' ') {
buffer[index--] = '0';
buffer[index--] = '2';
buffer[index--] = '%';
} else {
buffer[index--] = buffer[i];
}
}

System.out.println("\nAfter\n" + Arrays.toString(buffer));
System.out.println("New Size: " + newSize);

}

}