diff --git a/src/main/java/StringManipulation.java b/src/main/java/StringManipulation.java index 4deb6f1..2c7bb09 100644 --- a/src/main/java/StringManipulation.java +++ b/src/main/java/StringManipulation.java @@ -1,32 +1,79 @@ public class StringManipulation implements StringManipulationInterface { + private String myString = ""; @Override public String getString() { - return null; + return myString; } @Override public void setString(String string) { + myString = string; } @Override public int count() { - return 0; + String[] spl = myString.split("\\s+"); + int i = 0; + for (String str : spl) { + if (!str.equals("")) { + i++; + } + } + return i; } @Override public String removeNthCharacter(int n, boolean maintainSpacing) { - return null; + if (n > myString.length()) { + throw new IndexOutOfBoundsException(); + } else if (n <= 0) { + throw new IllegalArgumentException(); + } + char[] ar = myString.toCharArray(); + StringBuilder ret = new StringBuilder(); + for (int i = 1; i <= myString.length(); i++) { + if (i != 0 && i % n == 0) { + if (maintainSpacing) { + ret.append(" "); + } + } else { + ret.append(ar[i - 1]); + } + } + return ret.toString(); } @Override - public String[] getSubStrings(int startWord, int endWord) { - return null; + public String[] getSubStrings(int startWord, int endWord){ + if (startWord <= 0 || endWord <= 0 || endWord < startWord) { + throw new IllegalArgumentException(); + } + String[] ret = new String[endWord - startWord + 1]; + String[] spl = myString.split("\\s+"); + if (spl.length < endWord) { + throw new IndexOutOfBoundsException(); + } + for (int i = 0; i < ret.length; i++) { + ret[i] = spl[startWord - 1 + i]; + } + return ret; } @Override - public String restoreString(int[] indices) { - return null; + public String restoreString(int[] indices){ + if (indices.length != myString.length()) { + throw new IllegalArgumentException(); + } + StringBuilder ret = new StringBuilder(); + char[] ar = myString.toCharArray(); + for (int i = 0; i < indices.length; i++) { + if (indices[i] < 0 || indices[i] >= ar.length) { + throw new IndexOutOfBoundsException(); + } + ret.append(ar[indices[i]]); + } + return ret.toString(); } diff --git a/src/main/java/StringManipulationInterface.java b/src/main/java/StringManipulationInterface.java index 87127e7..cc81e80 100644 --- a/src/main/java/StringManipulationInterface.java +++ b/src/main/java/StringManipulationInterface.java @@ -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 @@ -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. diff --git a/src/test/java/StringManipulationTest.java b/src/test/java/StringManipulationTest.java index 6692c2c..7d43206 100644 --- a/src/test/java/StringManipulationTest.java +++ b/src/test/java/StringManipulationTest.java @@ -2,7 +2,6 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; - import static org.junit.jupiter.api.Assertions.*; public class StringManipulationTest { @@ -19,6 +18,9 @@ public void tearDown() { manipulatedstring = null; } + /** + * Check whether a simple string of space-separated words is counted correctly. + */ @Test public void testCount1() { manipulatedstring.setString("This is my string"); @@ -26,58 +28,99 @@ public void testCount1() { assertEquals(4, length); } + /** + * Check whether an empty string is correctly counted as zero. + */ @Test public void testCount2() { - fail("Not yet implemented"); + manipulatedstring.setString(""); + assertEquals(0, manipulatedstring.count()); } + /** + * Check whether a single letter string is correctly counted as one. + */ @Test public void testCount3() { - fail("Not yet implemented"); + manipulatedstring.setString("A"); + assertEquals(1, manipulatedstring.count()); } + /** + * Check whether a string of multiple spaces is correctly counted as zero. + */ @Test public void testCount4() { - fail("Not yet implemented"); + manipulatedstring.setString(" "); + assertEquals(0, manipulatedstring.count()); } + /** + * Check whether every third character is correctly removed (not maintaining spacing) in a simple scenario. + */ @Test public void testRemoveNthCharacter1() { manipulatedstring.setString("I'd b3tt3r put s0me d161ts in this 5tr1n6, right?"); assertEquals("I' bttr uts0e 16tsinths trn6 rgh?", manipulatedstring.removeNthCharacter(3, false)); } + /** + * Check whether every third character is correctly replaced with spaces in a simple scenario. + */ @Test public void testRemoveNthCharacter2() { manipulatedstring.setString("I'd b3tt3r put s0me d161ts in this 5tr1n6, right?"); assertEquals("I' b tt r ut s0 e 16 ts in th s tr n6 r gh ?", manipulatedstring.removeNthCharacter(3, true)); } + /** + * Check whether an out of bounds exception is correctly thrown when n is over the string length. + */ @Test public void testRemoveNthCharacter3() { - fail("Not yet implemented"); + manipulatedstring.setString("A b c d e"); + assertThrows(IndexOutOfBoundsException.class, () -> manipulatedstring.removeNthCharacter(10, false)); } + /** + * Check whether an illegal argument exception is correctly thrown when n is negative. + */ @Test public void testRemoveNthCharacter4() { - fail("Not yet implemented"); + manipulatedstring.setString("A b c d e"); + assertThrows(IllegalArgumentException.class, () -> manipulatedstring.removeNthCharacter(-1, false)); } + /** + * Check whether whitespace is removed correctly too. + */ @Test public void testRemoveNthCharacter5() { - fail("Not yet implemented"); + manipulatedstring.setString("A\nb\tc\nd\te\n"); + assertEquals("Abcde", manipulatedstring.removeNthCharacter(2, false)); } + /** + * Check whether a single character is correctly replaced with a single space if n is one and spacing is maintained. + */ @Test public void testRemoveNthCharacter6() { - fail("Not yet implemented"); + manipulatedstring.setString("a"); + assertEquals(" ", manipulatedstring.removeNthCharacter(1, true)); } + /** + * Check whether an illegal argument exception is correctly thrown if n is zero AND the string is empty. + */ @Test public void testRemoveNthCharacter7() { - fail("Not yet implemented"); + manipulatedstring.setString(""); + assertThrows(IllegalArgumentException.class, () -> manipulatedstring.removeNthCharacter(0, false)); } + /** + * Check whether a simple string of words is correctly turned into substrings. + */ @Test public void testGeSubStrings1() { manipulatedstring.setString("This is my string"); @@ -87,27 +130,66 @@ public void testGeSubStrings1() { assertEquals(sStings[1], "string"); } + /** + * Check whether an illegal argument exception is correctly thrown when one of the string indices is zero. + */ @Test public void testGeSubStrings2() { - fail("Not yet implemented"); + manipulatedstring.setString("a b c d"); + assertThrows(IllegalArgumentException.class, () -> manipulatedstring.getSubStrings(0, 1)); } + + /** + * Check whether an illegal argument exception is correctly thrown when the second index is lower than the first. + */ @Test public void testGeSubStrings3() { - fail("Not yet implemented"); + manipulatedstring.setString("a b c d"); + assertThrows(IllegalArgumentException.class, () -> manipulatedstring.getSubStrings(2, 1)); } + /** + * Check whether an out of bounds exception is correctly thrown when the right index is higher than the number of + * words. + */ @Test public void testGeSubStrings4() { - fail("Not yet implemented"); + manipulatedstring.setString("a b c d"); + assertThrows(IndexOutOfBoundsException.class, () -> manipulatedstring.getSubStrings(2, 5)); } + + /** + * Check whether a single string is correctly returned when the indices are the same and there is only + * one word. + */ @Test public void testGeSubStrings5() { - fail("Not yet implemented"); + manipulatedstring.setString("abc"); + assertEquals("abc", manipulatedstring.getSubStrings(1, 1)[0]); } + + /** + * Check whether an out of bounds exception is correctly thrown for a string of whitespace with nonzero indices. + */ @Test public void testGeSubStrings6() { - fail("Not yet implemented"); + manipulatedstring.setString(" "); + assertThrows(IndexOutOfBoundsException.class, () -> manipulatedstring.getSubStrings(1, 1)); } + /** + * Check whether a simple string with words separated by multiple spaces is correctly converted to substrings. + */ + @Test + public void testGeSubStrings7() { + manipulatedstring.setString("This is my string"); + String [] sStings = manipulatedstring.getSubStrings(3, 4); + assertEquals(sStings[0], "my"); + assertEquals(sStings[1], "string"); + } + + /** + * Check whether a simple string can be rearranged correctly. + */ @Test public void testRestoreString1() { @@ -118,32 +200,51 @@ public void testRestoreString1() assertEquals(restoreString, "rat"); } + /** + * Check whether a simple string can be correctly rearranged into all the same letter (this wasn't part of the spec + * but I am making the executive decision to include it). + */ @Test public void testRestoreString2() { - fail("Not yet implemented"); - + manipulatedstring.setString("abc"); + int [] array = new int[]{2,2,2}; + assertEquals(manipulatedstring.restoreString(array), "ccc"); } + /** + * Check whether an illegal argument exception is correctly thrown when the number of indices doesn't match + * the number of letters in the string. + */ @Test public void testRestoreString3() { - fail("Not yet implemented"); - + manipulatedstring.setString("abcd"); + int [] array = new int[]{1,2,3}; + assertThrows(IllegalArgumentException.class, () -> manipulatedstring.restoreString(array)); } - + /** + * Check whether an illegal argument exception is correctly thrown when one of the indices is higher than the + * string length. + */ @Test public void testRestoreString4() { - fail("Not yet implemented"); - + manipulatedstring.setString("abc"); + int [] array = new int[]{1,2,3}; + assertThrows(IndexOutOfBoundsException.class, () -> manipulatedstring.restoreString(array)); } + /** + * Check whether a string with non-alphanumeric characters can be successfully rearranged. (Also not part + * of the spec, but my implementation does it anyway and I wanted to test it.) + */ @Test public void testRestoreString5() { - fail("Not yet implemented"); - + manipulatedstring.setString("A 1"); + int [] array = new int[]{2,0,2}; + assertEquals(manipulatedstring.restoreString(array), "1A1"); } }