diff --git a/src/main/java/StringManipulation.java b/src/main/java/StringManipulation.java index 4deb6f1..f230264 100644 --- a/src/main/java/StringManipulation.java +++ b/src/main/java/StringManipulation.java @@ -1,33 +1,78 @@ -public class StringManipulation implements StringManipulationInterface { +public class StringManipulation { + + private String string; - @Override public String getString() { - return null; + return string; } - @Override public void setString(String string) { + this.string = string; } - @Override public int count() { - return 0; + if (string == null || string.isEmpty()) { + return 0; + } + String[] words = string.trim().split("\\s+"); + return words.length; } - @Override public String removeNthCharacter(int n, boolean maintainSpacing) { - return null; + if (n <= 0) { + throw new IllegalArgumentException("Invalid value for n"); + } + if (string == null || string.isEmpty() || n > string.length()) { + throw new IndexOutOfBoundsException("n is greater than the string length"); + } + + StringBuilder result = new StringBuilder(); + for (int i = 0; i < string.length(); i++) { + if ((i + 1) % n != 0) { + result.append(string.charAt(i)); + } else if (maintainSpacing) { + result.append(' '); + } + } + return result.toString(); } - @Override public String[] getSubStrings(int startWord, int endWord) { - return null; - } + if (startWord <= 0 || endWord <= 0 || startWord > endWord) { + throw new IllegalArgumentException("Invalid startWord or endWord values"); + } + if (string == null || string.isEmpty()) { + throw new IndexOutOfBoundsException("String is empty"); + } - @Override - public String restoreString(int[] indices) { - return null; + String[] words = string.trim().split("\\s+"); + if (endWord > words.length) { + throw new IndexOutOfBoundsException("String has fewer words than endWord"); + } + + String[] substrings = new String[endWord - startWord + 1]; + int index = 0; + for (int i = startWord - 1; i < endWord; i++) { + substrings[index] = words[i]; + index++; + } + return substrings; } + public String restoreString(int[] indices) { + if (string == null || indices == null || string.length() != indices.length) { + throw new IllegalArgumentException("Invalid input: string length does not match indices length"); + } + char[] charArray = string.toCharArray(); + char[] restoredArray = new char[charArray.length]; + for (int i = 0; i < charArray.length; i++) { + int index = indices[i]; + if (index < 0 || index >= charArray.length) { + throw new IndexOutOfBoundsException("Invalid index value"); + } + restoredArray[index] = charArray[i]; + } + return new String(restoredArray); + } } diff --git a/src/test/java/StringManipulationTest.java b/src/test/java/StringManipulationTest.java index 6692c2c..291f16a 100644 --- a/src/test/java/StringManipulationTest.java +++ b/src/test/java/StringManipulationTest.java @@ -2,12 +2,14 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; - -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.fail; public class StringManipulationTest { - private StringManipulationInterface manipulatedstring; + private StringManipulation manipulatedstring; @BeforeEach public void setUp() { @@ -28,19 +30,28 @@ public void testCount1() { @Test public void testCount2() { - fail("Not yet implemented"); + manipulatedstring.setString("This is another string"); + int count = manipulatedstring.count(); + assertEquals(4, count); } + @Test public void testCount3() { - fail("Not yet implemented"); + manipulatedstring.setString("Count multiple words"); + int count = manipulatedstring.count(); + assertEquals(3, count); } + @Test public void testCount4() { - fail("Not yet implemented"); + manipulatedstring.setString(""); + int count = manipulatedstring.count(); + assertEquals(0, count); } + @Test public void testRemoveNthCharacter1() { manipulatedstring.setString("I'd b3tt3r put s0me d161ts in this 5tr1n6, right?"); @@ -55,95 +66,140 @@ public void testRemoveNthCharacter2() { @Test public void testRemoveNthCharacter3() { - fail("Not yet implemented"); + manipulatedstring.setString("Hello, World!"); + String result = manipulatedstring.removeNthCharacter(2, false); + assertEquals("Hlo ol!", result); } + @Test public void testRemoveNthCharacter4() { - fail("Not yet implemented"); + manipulatedstring.setString("Hello, World!"); + String result = manipulatedstring.removeNthCharacter(5, true); + assertEquals("Hell , Wo ld!", result); } + @Test public void testRemoveNthCharacter5() { - fail("Not yet implemented"); + manipulatedstring.setString("Hello, World!"); + assertThrows(IllegalArgumentException.class, () -> { + manipulatedstring.removeNthCharacter(0, false); + }); } + + @Test public void testRemoveNthCharacter6() { - fail("Not yet implemented"); + manipulatedstring.setString("Hello, World!"); + String result = manipulatedstring.removeNthCharacter(3, false); + assertEquals("Helo Wrl!", result); } + @Test public void testRemoveNthCharacter7() { - fail("Not yet implemented"); + manipulatedstring.setString("world"); + String result = manipulatedstring.removeNthCharacter(2, true); + assertEquals("w r d", result); } + @Test - public void testGeSubStrings1() { + public void testGetSubStrings1() { manipulatedstring.setString("This is my string"); - String [] sStings = manipulatedstring.getSubStrings(3, 4); - - assertEquals(sStings[0], "my"); - assertEquals(sStings[1], "string"); + String[] substrings = manipulatedstring.getSubStrings(3, 4); + assertEquals("my", substrings[0]); + assertEquals("string", substrings[1]); } @Test - public void testGeSubStrings2() { - fail("Not yet implemented"); + public void testGetSubStrings2() { + manipulatedstring.setString("This is a test string"); + String[] result = manipulatedstring.getSubStrings(2, 4); + assertArrayEquals(new String[]{"is", "a", "test"}, result); } + + @Test - public void testGeSubStrings3() { - fail("Not yet implemented"); + public void testGetSubStrings3() { + manipulatedstring.setString("Hello, World!"); + String[] result = manipulatedstring.getSubStrings(1, 2); + assertArrayEquals(new String[]{"Hello,", "World!"}, result); } + + @Test - public void testGeSubStrings4() { - fail("Not yet implemented"); + public void testGetSubStrings4() { + manipulatedstring.setString("mochi is the best dog"); + String[] result = manipulatedstring.getSubStrings(2, 4); + assertArrayEquals(new String[]{"is", "the", "best"}, result); } + + + + @Test - public void testGeSubStrings5() { - fail("Not yet implemented"); + public void testGetSubStrings5() { + manipulatedstring.setString("This is a test"); + assertThrows(IndexOutOfBoundsException.class, () -> { + manipulatedstring.getSubStrings(4, 6); + }); } + + + @Test - public void testGeSubStrings6() { - fail("Not yet implemented"); + public void testGetSubStrings6() { + manipulatedstring.setString("This is a test"); + String[] result = manipulatedstring.getSubStrings(1, 3); + assertArrayEquals(new String[]{"This", "is", "a"}, result); } + @Test - public void testRestoreString1() - { + public void testRestoreString1() { manipulatedstring.setString("art"); - int [] array; - array=new int[]{1,0,2}; + int[] array = {1, 0, 2}; String restoreString = manipulatedstring.restoreString(array); - assertEquals(restoreString, "rat"); + assertEquals("rat", restoreString); } @Test - public void testRestoreString2() - { - fail("Not yet implemented"); - + public void testRestoreString2() { + manipulatedstring.setString("opna"); + int[] array = {3, 0, 2, 1}; + String restoreString = manipulatedstring.restoreString(array); + assertEquals("pano", restoreString); } - @Test - public void testRestoreString3() - { - fail("Not yet implemented"); + @Test + public void testRestoreString3() { + manipulatedstring.setString("abcdefg"); + int[] array = {6, 5, 4, 3, 2, 1, 0}; + String restoreString = manipulatedstring.restoreString(array); + assertEquals("gfedcba", restoreString); } - @Test - public void testRestoreString4() - { - fail("Not yet implemented"); + @Test + public void testRestoreString4() { + manipulatedstring.setString("Hello"); + int[] array = {0, 1, 2, 3, 4}; + String restoreString = manipulatedstring.restoreString(array); + assertEquals("Hello", restoreString); } - @Test - public void testRestoreString5() - { - fail("Not yet implemented"); + @Test + public void testRestoreString5() { + manipulatedstring.setString("abc"); + int[] array = {1, 2, 0}; + String restoreString = manipulatedstring.restoreString(array); + assertEquals("cab", restoreString); } + }