Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question Solution Using Descending For loop #1

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
25 changes: 18 additions & 7 deletions src/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ with a buffer (similar to how a resizable ArrayList, and/or a
CareerCup, Palo Alto, CA.
*/


import java.util.Arrays;

public class Main {
Expand All @@ -48,7 +47,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 = "This starts as 38 and ends up with 54.";
for (int i = 0; i < temp.length(); i++) {
buffer[i] = temp.charAt(i);
}
Expand All @@ -59,14 +58,26 @@ 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));
System.out.println("size: " + size);
}

// write your method here


public static int replaceSpaces(char[] buffer, int size){
for(int i = 0; i < size; i++){
if(buffer[i]==' '){
for(int j=size+3; j>=i ;j--){
buffer[j+2] = buffer[j];
}
buffer[i]='%';
buffer[i+1] = '2';
buffer[i+2] = '0';
size=size+2;
}
}
return size;
}
}