diff --git a/src/main/java/StringManipulation.java b/src/main/java/StringManipulation.java index 4deb6f1..7e8dbae 100644 --- a/src/main/java/StringManipulation.java +++ b/src/main/java/StringManipulation.java @@ -1,33 +1,129 @@ public class StringManipulation implements StringManipulationInterface { + private String currString; + private int stringLength; + @Override public String getString() { - return null; + return currString; } @Override public void setString(String string) { + currString = string; + stringLength = currString.length(); } @Override public int count() { - return 0; + +if(currString.length()==0)//empty string has no words + return 0; + + int wordCount = 0; + // Count the number of spaces to determine the number of words + for (int i = 0; i < stringLength; i++) { + if (currString.charAt(i) == ' ') { + wordCount++; + } + } + + // Add 1 to the word count to account for the last word + return wordCount + 1; } @Override public String removeNthCharacter(int n, boolean maintainSpacing) { - return null; + if (n <= 0) { + throw new IllegalArgumentException("n is less than or equal to 0"); + } + + if (n > stringLength) { + throw new IndexOutOfBoundsException("n is greater than the length of the string"); + } + + String newString = ""; + + // Iterate through each character in the string + for (int i = 1; i <= stringLength; i++) { + // Check if the current position is a multiple of n + if (i % n == 0) { + // If maintainSpacing is true, add a space character + if (maintainSpacing) { + newString += ' '; + } + } else { + // Otherwise, add the character at the current position to the new string + newString += currString.charAt(i - 1); + } + } + + return newString; } @Override public String[] getSubStrings(int startWord, int endWord) { - return null; + if (startWord <= 0 || endWord <= 0 || startWord > endWord) { + throw new IllegalArgumentException("Invalid parameters"); + } + + if (count() < endWord) { + throw new IndexOutOfBoundsException("endWord is greater than the total number of words in the string"); + } + + String[] subStrings = new String[endWord - startWord + 1]; + int k = 0; + String currWord = ""; + int currWordIndex = 0; + + // Iterate through each character in the string + for (int i = 0; i < stringLength; i++) { + if (currString.charAt(i) == ' ') { + currWordIndex++; + + // Check if the current word index is within the specified range + if (currWordIndex >= startWord && currWordIndex <= endWord) { + subStrings[k++] = currWord; + } + + currWord = ""; + } else { + currWord += currString.charAt(i); + } + } + + currWordIndex++; + + // Check if the last word is within the specified range + if (currWordIndex >= startWord && currWordIndex <= endWord) { + subStrings[k++] = currWord; + } + + return subStrings; } @Override public String restoreString(int[] indices) { - return null; - } + if (stringLength != indices.length) { + throw new IllegalArgumentException("Size of indices mismatches the size of the current string"); + } + + String shuffledString = new String(currString); + char[] shuffledArray = shuffledString.toCharArray(); + + // Iterate through each index in the indices array + for (int i = 0; i < stringLength; i++) { + // Check if the index is valid + if (indices[i] < 0 || indices[i] >= stringLength) { + throw new IndexOutOfBoundsException("Invalid index " + indices[i] + " in the indices array"); + } + // Restore the character at the corresponding index + shuffledArray[indices[i]] = currString.charAt(i); + } + shuffledString = new String(shuffledArray); + + return shuffledString; + } } 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..06a8c63 100644 --- a/src/test/java/StringManipulationTest.java +++ b/src/test/java/StringManipulationTest.java @@ -2,148 +2,196 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; - import static org.junit.jupiter.api.Assertions.*; public class StringManipulationTest { - private StringManipulationInterface manipulatedstring; + private StringManipulationInterface manipulatedString; @BeforeEach public void setUp() { - manipulatedstring = new StringManipulation(); + manipulatedString = new StringManipulation(); } @AfterEach public void tearDown() { - manipulatedstring = null; + manipulatedString = null; } @Test public void testCount1() { - manipulatedstring.setString("This is my string"); - int length = manipulatedstring.count(); + manipulatedString.setString("This is my string"); + int length = manipulatedString.count(); assertEquals(4, length); } @Test public void testCount2() { - fail("Not yet implemented"); + manipulatedString.setString("One Two Three"); + int length = manipulatedString.count(); + assertEquals(3, length); } @Test public void testCount3() { - fail("Not yet implemented"); + manipulatedString.setString(""); + int length = manipulatedString.count(); + assertEquals(0, length); } @Test public void testCount4() { - fail("Not yet implemented"); + manipulatedString.setString("SingleWord"); + int length = manipulatedString.count(); + assertEquals(1, length); } @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)); + manipulatedString.setString("I'd b3tt3r put s0me d161ts in this 5tr1n6, right?"); + String result = manipulatedString.removeNthCharacter(3, false); + assertEquals("I' bttr uts0e 16tsinths trn6 rgh?", result); } @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)); + manipulatedString.setString("I'd b3tt3r put s0me d161ts in this 5tr1n6, right?"); + String result = manipulatedString.removeNthCharacter(3, true); + assertEquals("I' b tt r ut s0 e 16 ts in th s tr n6 r gh ?", result); } @Test public void testRemoveNthCharacter3() { - fail("Not yet implemented"); + manipulatedString.setString(""); + assertThrows(IndexOutOfBoundsException.class, () -> { + manipulatedString.removeNthCharacter(5, true); + }); } @Test public void testRemoveNthCharacter4() { - fail("Not yet implemented"); + manipulatedString.setString("12345"); + assertThrows(IndexOutOfBoundsException.class, () -> { + manipulatedString.removeNthCharacter(6, false); + }); } @Test public void testRemoveNthCharacter5() { - fail("Not yet implemented"); + manipulatedString.setString("abcdefg"); + assertThrows(IllegalArgumentException.class, () -> { + manipulatedString.removeNthCharacter(0, true); + }); } - @Test public void testRemoveNthCharacter6() { - fail("Not yet implemented"); + manipulatedString.setString("abcdefg"); + assertThrows(IllegalArgumentException.class, () -> { + manipulatedString.removeNthCharacter(-5, true); + }); } - @Test public void testRemoveNthCharacter7() { - fail("Not yet implemented"); + manipulatedString.setString("abcdefg"); + assertThrows(IndexOutOfBoundsException.class, () -> { + manipulatedString.removeNthCharacter(5000, true); + }); } @Test - public void testGeSubStrings1() { - manipulatedstring.setString("This is my string"); - String [] sStings = manipulatedstring.getSubStrings(3, 4); + public void testGetSubStrings1() { + manipulatedString.setString("This is my string"); + String[] subStrings = manipulatedString.getSubStrings(3, 4); - assertEquals(sStings[0], "my"); - assertEquals(sStings[1], "string"); + 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[] subStrings = manipulatedString.getSubStrings(1, 3); + + assertEquals("This", subStrings[0]); + assertEquals("is", subStrings[1]); + assertEquals("a", subStrings[2]); } + @Test - public void testGeSubStrings3() { - fail("Not yet implemented"); + public void testGetSubStrings3() { + manipulatedString.setString("Just one word"); + String[] subStrings = manipulatedString.getSubStrings(1, 1); + + assertEquals("Just", subStrings[0]); } + @Test - public void testGeSubStrings4() { - fail("Not yet implemented"); + public void testGetSubStrings4() { + manipulatedString.setString("No Words"); + assertThrows(IllegalArgumentException.class, () -> { + manipulatedString.getSubStrings(2, 1); + }); } + @Test - public void testGeSubStrings5() { - fail("Not yet implemented"); + public void testGetSubStrings5() { + manipulatedString.setString(""); + assertThrows(IndexOutOfBoundsException.class, () -> { + manipulatedString.getSubStrings(1, 3); + }); } + @Test - public void testGeSubStrings6() { - fail("Not yet implemented"); + public void testGetSubStrings6() { + manipulatedString.setString("Testing multiple words"); + String[] subStrings = manipulatedString.getSubStrings(1, 3); + + assertEquals("Testing", subStrings[0]); + assertEquals("multiple", subStrings[1]); + assertEquals("words", subStrings[2]); } + + @Test - public void testRestoreString1() - { - manipulatedstring.setString("art"); - int [] array; - array=new int[]{1,0,2}; - String restoreString = manipulatedstring.restoreString(array); - assertEquals(restoreString, "rat"); + public void testRestoreString1() { + manipulatedString.setString("art"); + int[] indices = new int[]{1, 0, 2}; + String restoredString = manipulatedString.restoreString(indices); + assertEquals("rat", restoredString); } @Test - public void testRestoreString2() - { - fail("Not yet implemented"); - + public void testRestoreString2() { + manipulatedString.setString("abcde"); + int[] indices = new int[]{4, 3, 2, 1, 0}; + String restoredString = manipulatedString.restoreString(indices); + assertEquals("edcba", restoredString); } @Test - public void testRestoreString3() - { - fail("Not yet implemented"); - + public void testRestoreString3() { + manipulatedString.setString("Hello"); + int[] indices = new int[]{0, 1, 2, 3, 4}; + String restoredString = manipulatedString.restoreString(indices); + assertEquals("Hello", restoredString); } @Test - public void testRestoreString4() - { - fail("Not yet implemented"); - + public void testRestoreString4() { + manipulatedString.setString("12345"); + int[] indices = new int[]{0, 4, 2, 1, 3}; + String restoredString = manipulatedString.restoreString(indices); + assertEquals("14352", restoredString); } @Test - public void testRestoreString5() - { - fail("Not yet implemented"); - + public void testRestoreString5() { + manipulatedString.setString(""); + int[] indices = new int[]{}; + String restoredString = manipulatedString.restoreString(indices); + assertEquals("", restoredString); } + }