diff --git a/src/main/java/StringManipulation.java b/src/main/java/StringManipulation.java index 4deb6f1..75e1038 100644 --- a/src/main/java/StringManipulation.java +++ b/src/main/java/StringManipulation.java @@ -1,33 +1,120 @@ public class StringManipulation implements StringManipulationInterface { + 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 (this.string == null){ + throw new NullPointerException("NullPointerException"); + } + + if (this.string.isEmpty()){ + return 0; + } + + String[] result = this.string.split("\s+"); + int word = 0; + for (String current : result) { + if (!current.isEmpty()) { + word++; + } + } + return word; } @Override public String removeNthCharacter(int n, boolean maintainSpacing) { - return null; + + if (this.string == null || this.string.isEmpty()){ + throw new NullPointerException("NullPointerException"); + } + int length = string.length(); + + if (n > string.length()) { + throw new IndexOutOfBoundsException("Out Of Bound"); +// System.out.println("Out Of Bound"); + } + + if (n <= 0) { + throw new IllegalArgumentException("Invalid"); + } + + StringBuilder result = new StringBuilder(); + + for (int index = 0; index < length; index++) { + int compare = index + 1; + + if (compare % n != 0) { + result.append(string.charAt(index)); + + } else if (maintainSpacing == true) { + 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"); + } + //String sentence = "This is an example sentence"; + String[] words = this.string.split(" "); + + if (endWord > words.length) { + throw new IndexOutOfBoundsException("Not enough words in the sentence"); + } + + 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; } + @Override public String restoreString(int[] indices) { - return null; - } + if (this.string.isEmpty()){ + return ""; + } + + if ( indices.length != string.length()) { + throw new IllegalArgumentException("Invalid"); + } + if (string == null || indices == null) { + throw new NullPointerException("NullPointerException"); + } + + char[] restoredChars = new char[string.length()]; + + for (int i = 0; i < indices.length; i++) { + int index = indices[i]; + if (index < 0 || index >= string.length()) { + throw new IndexOutOfBoundsException("Not enough words in the sentence"); + } + restoredChars[index] = string.charAt(i); + } + + return new String(restoredChars); + } } + 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..2d18e98 100644 --- a/src/test/java/StringManipulationTest.java +++ b/src/test/java/StringManipulationTest.java @@ -1,3 +1,4 @@ +import jdk.jfr.StackTrace; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -28,17 +29,30 @@ public void testCount1() { @Test public void testCount2() { - fail("Not yet implemented"); + manipulatedstring.setString(""); + int length = manipulatedstring.count(); + assertEquals(0, length); } @Test public void testCount3() { - fail("Not yet implemented"); + assertThrows(NullPointerException.class, () -> { + //int length = + manipulatedstring.count(); + }); } @Test public void testCount4() { - fail("Not yet implemented"); + manipulatedstring.setString("Spaces between words "); + int length = manipulatedstring.count(); + assertEquals(3, length); + } + + public void testCount5() { + manipulatedstring.setString(" ABCDEFD "); + int length = manipulatedstring.count(); + assertEquals(1, length); } @Test @@ -52,32 +66,49 @@ 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)); } - @Test public void testRemoveNthCharacter3() { - fail("Not yet implemented"); + //null + assertThrows(NullPointerException.class,() ->{ + manipulatedstring.removeNthCharacter(3, true); + }); } + @Test - public void testRemoveNthCharacter4() { - fail("Not yet implemented"); + public void testRemoveNthCharacter4(){ + assertThrows(IllegalArgumentException.class, () -> { + manipulatedstring.setString("BUBU"); + manipulatedstring.removeNthCharacter(-3, true); + }); } @Test - public void testRemoveNthCharacter5() { - fail("Not yet implemented"); + public void testRemoveNthCharacter5(){ + assertThrows(IndexOutOfBoundsException.class, () -> { + manipulatedstring.setString("love"); + manipulatedstring.removeNthCharacter(7, true); + }); } @Test - public void testRemoveNthCharacter6() { - fail("Not yet implemented"); + public void testRemoveNthCharacter6(){ + assertThrows(IllegalArgumentException.class, () -> { + manipulatedstring.setString(" "); + manipulatedstring.removeNthCharacter(0, true); + }); } @Test - public void testRemoveNthCharacter7() { - fail("Not yet implemented"); + public void testRemoveNthCharacter7(){ + assertThrows(NullPointerException.class, () -> { + manipulatedstring.setString(""); //empty + manipulatedstring.removeNthCharacter(0,true); + + }); } + @Test public void testGeSubStrings1() { manipulatedstring.setString("This is my string"); @@ -88,26 +119,42 @@ public void testGeSubStrings1() { } @Test - public void testGeSubStrings2() { - fail("Not yet implemented"); - } - @Test - public void testGeSubStrings3() { - fail("Not yet implemented"); + public void testGetSubStrings2() { + assertThrows(IllegalArgumentException.class, () -> { + manipulatedstring.setString("OnlyOneWord"); + // String[] subStrings = + manipulatedstring.getSubStrings(1, -1); + }); } + @Test - public void testGeSubStrings4() { - fail("Not yet implemented"); + public void testGetSubStrings3() { + assertThrows(IndexOutOfBoundsException.class, () -> { + manipulatedstring.setString("One Two Three Four Five"); + String[] subStrings = manipulatedstring.getSubStrings(4, 6); + + assertEquals("Four", subStrings[0]); + assertEquals("Five", subStrings[1]); + }); } + @Test - public void testGeSubStrings5() { - fail("Not yet implemented"); + public void testGetSubStrings4() { + assertThrows(IllegalArgumentException.class, () -> { + manipulatedstring.setString("One Two Three Four Five"); + String[] subStrings = manipulatedstring.getSubStrings(50,1); + }); } + @Test - public void testGeSubStrings6() { - fail("Not yet implemented"); + public void testGetSubString5() { + assertThrows(IllegalArgumentException.class, () -> { + manipulatedstring.setString("Programming"); + String[] subStrings = manipulatedstring.getSubStrings(0, 0); + }); } + @Test public void testRestoreString1() { @@ -119,31 +166,42 @@ public void testRestoreString1() } @Test - public void testRestoreString2() - { - fail("Not yet implemented"); - + public void testRestoreString2() { + assertThrows(IllegalArgumentException.class, () -> { + manipulatedstring.setString("testing"); + int[] indices = {}; + //String restoredString = + manipulatedstring.restoreString(indices); + }); } @Test - public void testRestoreString3() - { - fail("Not yet implemented"); - + public void testRestoreString3() { + assertThrows(NullPointerException.class,() ->{ + int[] indices = {}; + //String restoredString = + manipulatedstring.restoreString(indices); + }); } @Test - public void testRestoreString4() - { - fail("Not yet implemented"); - + public void testRestoreString4() { + assertThrows(IndexOutOfBoundsException.class, () -> { + manipulatedstring.setString("hello"); + int[] indices = {4, 3, 2, 1, -1}; + // String restoredString = + manipulatedstring.restoreString(indices); + }); } @Test - public void testRestoreString5() - { - fail("Not yet implemented"); - + public void testRestoreString5() { + assertThrows(IndexOutOfBoundsException.class, () -> { + manipulatedstring.setString("world"); + int[] indices = {0, 2, 1, 3, 5}; + //String restoredString = + manipulatedstring.restoreString(indices); + }); } }