-
Notifications
You must be signed in to change notification settings - Fork 11
added my implementation to skeleton #5
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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() { | ||
|
|
@@ -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){ | ||
| return 0; | ||
| } | ||
|
|
||
| int countWords = 0; | ||
| if(myString.equals("")){ | ||
| return countWords; | ||
| } | ||
|
|
||
| String removeSpace = myString.trim(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
| } | ||
|
|
||
| } | ||
There was a problem hiding this comment.
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.