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
102 changes: 91 additions & 11 deletions src/main/java/StringManipulation.java
Original file line number Diff line number Diff line change
@@ -1,33 +1,113 @@
public class StringManipulation implements StringManipulationInterface {
/* Nadav Horowitz 6/3/2023 StringManipulation.java
* This file contains various methods for manipulation of a String object.
* The methods implemented include count, removeNthCharacter, getSubStrings, and restoreString.
*/
import java.util.Arrays;

public class StringManipulation implements StringManipulationInterface {

public String string;


@Override
//Getter method for String field
public String getString() {
return null;
return this.string;
}


@Override
//Setter method for String field
public void setString(String string) {
this.string = string;
}


@Override
//Returns number of words present in String object
public int count() {
return 0;
//null case and empty case
if (string == null || string.length() == 0)
return 0;
//trim leading and ending whitespace
String copy = this.getString();

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Since the string is already a member of the class, there's no need to call a getter method of getString() to access it, nor is there any reason to copy the string every time -- none of these methods are mutators.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I make a copy here because afterwards I call the String.trim() method, which removes leading and lagging whitespace. Just for the sake of not changing the original string.

copy = copy.trim();
//Regex for matching words
String[] words = copy.split(" +");
return words.length;
}


Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This code shows good practice in terms of readability, by using specific and detailed comments before each method header in the class.

@Override
//Returns a string that consists of all characters in the original string except for the characters
//in positions n, 2n, 3n, and so on, either deleting those or replacing them with a white space.
//Throws IllegalArgumentException for n <= 0, throws IndexOutOfBoundsException for n > string length.
public String removeNthCharacter(int n, boolean maintainSpacing) {
return null;
if(n <= 0)
throw new IllegalArgumentException();
if(n > string.length())
throw new IndexOutOfBoundsException();

String replacement = "";
if(maintainSpacing)
replacement += " ";

String newString = "";
for(int i = 1; i <= string.length(); i++){
char next = string.charAt(i-1);
if(i % n == 0)
newString += replacement;
else
newString += next;
}
return newString;
}


@Override
public String[] getSubStrings(int startWord, int endWord) {
return null;
//Returns String[] containing words from position "startWord" to position "endWord" in String object,
//with 1 being the first Word in the String.
//Throws IllegalArgumentException If either "startWord" or "endWord" are invalid.
//Throws IndexOutOfBoundsException If the string has less than "endWord" words in it.
public String[] getSubStrings(int startWord, int endWord){
if(startWord <= 0 || endWord <= 0 || startWord > endWord)
throw new IllegalArgumentException();

//trim leading and ending whitespace
String copy = this.getString();
copy = copy.trim();

//Regex for matching words
String[] words = copy.split(" +");

if(endWord > words.length)
throw new IndexOutOfBoundsException();

words = Arrays.copyOfRange(words,startWord - 1, endWord);
return words;
}


@Override
public String restoreString(int[] indices) {
return null;
//Given current String object and integer array "indices" of the same length, String will be shuffled such that
//the character at the ith position moves to indices[i] in the shuffled string. Returns the shuffled string.
//Throws IllegalArgumentException if string.length != indices.length
//Throws IndexOutOfBoundsException if indices[i]< 0 or indices[i]> string length.
public String restoreString(int[] indices){
String copy = this.getString();

if(copy.length() != indices.length)
throw new IllegalArgumentException();

String newString = "";
for(int i = 0; i < indices.length; i++) {
int stringIndex = indices[i];

if(stringIndex < 0 || stringIndex > indices.length)
throw new IndexOutOfBoundsException();

newString += copy.charAt(stringIndex);
}
return newString;
}


}
}
6 changes: 3 additions & 3 deletions src/main/java/StringManipulationInterface.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,10 @@ public interface StringManipulationInterface {
* The string s will be shuffled such that the character at the ith position moves to indices[i] in the shuffled string.
* Return the shuffled string.
* example:
* Input: string = "UnitTest", indices = [4,5,6,7,0,1,2,3]
* Input: string = "UnitTest", indices = [4,5,6,7,0,2,1,3]
* Output: "TestUnit"
* Explanation:
* indices: 4 5 6 7 0 1 2 3
* indices: 4 5 6 7 0 2 1 3
* String: U n i t T e s t
* Actions to Shuffle: Shift U to 4th position, n to 5th position, i to 6th position ......
* Output: T e s t U n i t
Expand All @@ -95,7 +95,7 @@ public interface StringManipulationInterface {
* indices length is the same as the string length.
*
* throws IllegalArgumentException if not s.length == indices.length == n
* throws IndexOutOfBoundsException if indices[i]< 0 or indices[i]>= string length
* throws IndexOutOfBoundsException if indices[i]< 0 or indices[i]> string length
*
* @param indices is an integer array for shuffled string new indices positions
* the character at the ith position moves to indices[i] in the shuffled string.
Expand Down
Loading