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
108 changes: 101 additions & 7 deletions src/main/java/StringManipulation.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
public class StringManipulation implements StringManipulationInterface {
private String myString;

@Override
public String getString() {
Expand All @@ -7,27 +8,120 @@ public String getString() {

@Override
public void setString(String string) {
this.myString = string;
}

@Override
public int count() {
return 0;
if(myString == null){

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line 16 and 21 can be combined since both will always result in countWords being zero.

return 0;
}

int countWords = 0;
if(myString.equals("")){
return countWords;
}

String removeSpace = myString.trim();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like you could potentially check if removeSpace.equals("") could be checked before going into the for loop since .trim() should remove all the leading and trailing spaces. In the case of the string "_ _ _ _" (spacing between _'s used to clarify example), spaces would be removed, thus leaving you a string "" and you can return before the loop.

for(String word : removeSpace.split("\\s+")){
if(removeSpace.equals(""))
return 0;
countWords++;
}

return countWords;
}

@Override
public String removeNthCharacter(int n, boolean maintainSpacing) {
return null;
if (myString.equals("")) {
return myString;
} else if(myString == null){
throw new NullPointerException("String is null.");
} else if(n <= 0){
throw new IllegalArgumentException("n cannot be less than or equal to zero");
} else if(n > myString.length()){
throw new IndexOutOfBoundsException("n cannot be greater than string length");
} else if(n == 1 && maintainSpacing){
return myString;
}

StringBuilder result = new StringBuilder();
int count = 0;

for (int i = 0; i < myString.length(); i++) {
char c = myString.charAt(i);

if (maintainSpacing) {
count++;

if (count % n != 0) {
result.append(c);
} else {
result.append(' ');
}
} else {
count++;
if (count % n != 0) {
result.append(c);
}
}
}
return result.toString();
}

@Override
public String[] getSubStrings(int startWord, int endWord) {
return null;
public String[] getSubStrings(int startWord, int endWord){
if(myString == null) {
throw new NullPointerException("String is null.");
} else if(startWord > endWord){
throw new IllegalArgumentException("startWord cannot be greater than endWord.");
} else if(startWord <= 0){
throw new IllegalArgumentException("startWord cannot be less than or equal to 0.");
} else if(endWord <= 0){
throw new IllegalArgumentException("endWord cannot be less than or equal to 0.");
}

//handles exception for endWord># of words in string
String[] words = myString.split("\\s+");
if(endWord > words.length){
throw new IndexOutOfBoundsException("endWord is greater than length of string.");
}

int size = endWord - startWord + 1;
String[] result = new String[size];
int index = 0;

for (int i = startWord-1; i < endWord; i++) {
result[index] = words[i];
index++;
}

return result;
}

@Override
public String restoreString(int[] indices) {
return null;
}
public String restoreString(int[] indices){
if(indices.length != myString.length()){
throw new IllegalArgumentException("Indices length does not equal string's length");
} else if(myString == null){
throw new NullPointerException("String is null.");
}

StringBuilder result = new StringBuilder(myString.length());

for(int index : indices){

if(index < 0 || index > myString.length()){
throw new IndexOutOfBoundsException("Indices length cannot be less than 0");
} else if(myString.equals("")){
return result.toString();
} else {
result.append(myString.charAt(index));
}
}

return result.toString();
}

}
Loading