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
97 changes: 90 additions & 7 deletions src/main/java/StringManipulation.java
Original file line number Diff line number Diff line change
@@ -1,33 +1,116 @@
// Nick Cudd
// CS 410 Spring 2023
// JUnit Testing Assignment

import java.util.ArrayList;
import java.util.HashMap;
import java.util.regex.Pattern;

public class StringManipulation implements StringManipulationInterface {

private String myString;

@Override
public String getString() {
return null;
return myString;
}

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

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.myString as it is a property of the class.

}

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Input Validation: The code correctly checks if the input string (myString) is null or empty. Returning 0 in these cases seems appropriate, assuming that an empty string should not be counted as a word.

}
int wordCount = 0;
// Use both whitespace and half thin-space characters to slice strings
String[] tokens = myString.split(" | ");
for (String i : tokens) {
if (Pattern.matches("^[A-Za-z]+$", i)) {
wordCount++;
}
}
return wordCount;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The code iterates over the tokens and checks if each token consists only of alphabetical characters using a regular expression pattern. This pattern matches strings that contain only one or more uppercase or lowercase letters. This approach assumes that words are defined as sequences of alphabetic characters without any punctuation or numbers. If this definition aligns with your requirements, then the code is suitable.

}

@Override
public String removeNthCharacter(int n, boolean maintainSpacing) {
return null;
if (n <= 0) {
throw new IllegalArgumentException("n is invalid: n > 0");
} else if (myString == null) {
throw new NullPointerException("String has not been initialized yet, or is null");
} else if (n > myString.length()) {
throw new IndexOutOfBoundsException("n cannot exceed length of parameter String");
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

throws appropriate exceptions for invalid cases.

ArrayList<Character> characters = new ArrayList<Character>();

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Instead of new ArrayList(), you can use new ArrayList<>() to let the compiler infer the type.

for (char c : myString.toCharArray()) {
characters.add(c);
}

for (int position = n, k = 2; position <= myString.length(); position = k * n, k++) {
if (maintainSpacing) {
characters.set((position - 1), ' ');
} else
characters.set((position - 1), null);
}

String newString = "";
for (Character c: characters) {
if (c != null) {
newString += c;
}
}
return newString;
}

@Override
public String[] getSubStrings(int startWord, int endWord) {
return null;
if (myString == null) {
throw new NullPointerException("String has not been initialized yet, or is null");
} else if (startWord <= 0 || endWord <= 0) {
throw new IllegalArgumentException("startWord and endWord cannot be less than 1");
} else if (endWord < startWord) {
throw new IndexOutOfBoundsException("index of endWord cannot be prior to index of startWord");
}

int numberOfWords = (endWord - startWord) + 1;
String[] words = myString.split(" | ");
String[] subWords = new String[numberOfWords];
for (int i = 0, j = startWord - 1; i < numberOfWords; i++, j++) {
subWords[i] = words[j];

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Instead of using a fixed-size array subWords, consider using a List implementation, such as an ArrayList, to allow for dynamic resizing as needed. This eliminates the need to calculate the number of words beforehand.

}
return subWords;
}

@Override
public String restoreString(int[] indices) {
return null;
if (myString == null) {
throw new NullPointerException("String has not been initialized yet, or is null");
}
// Examine passed indices array for validity
if (indices.length != myString.length()) {
throw new IllegalArgumentException("passed index array must be same length as current String");
}
for (int i = 0; i < indices.length - 1; i++) {
if (indices[i] < 0 || indices[i] > myString.length()) {
throw new IndexOutOfBoundsException("Array of indices cannot contain negative integers or integers greater than the length of the current String");
}
// Check for duplicate integers
for (int j = i + 1; j < indices.length; j++) {
if (indices[i] == indices[j]) {
throw new IllegalArgumentException("passed index array must contain unique integer values");
}
}
}
char[] charString = myString.toCharArray();
char[] referenceString = myString.toCharArray();
for (int i = 0; i < charString.length; i++) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The code creates two separate character arrays, charString and referenceString, from myString.toCharArray(). However, you can directly modify charString in place without the need for the referenceString array.

charString[i] = referenceString[indices[i]];
}
return new String(charString);
}


}
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