diff --git a/src/main/java/StringManipulation.java b/src/main/java/StringManipulation.java deleted file mode 100644 index 4deb6f1..0000000 --- a/src/main/java/StringManipulation.java +++ /dev/null @@ -1,33 +0,0 @@ -public class StringManipulation implements StringManipulationInterface { - - @Override - public String getString() { - return null; - } - - @Override - public void setString(String string) { - } - - @Override - public int count() { - return 0; - } - - @Override - public String removeNthCharacter(int n, boolean maintainSpacing) { - return null; - } - - @Override - public String[] getSubStrings(int startWord, int endWord) { - return null; - } - - @Override - public String restoreString(int[] indices) { - return null; - } - - -} diff --git a/src/main/java/StringManipulationInterface.java b/src/main/java/StringManipulationInterface.java deleted file mode 100644 index 87127e7..0000000 --- a/src/main/java/StringManipulationInterface.java +++ /dev/null @@ -1,108 +0,0 @@ -/** - * This is an interface for a simple class that represents a string, defined - * as a sequence of characters. - * - */ -public interface StringManipulationInterface { - - /** - * Returns the current string. If the string is null, it should return null. - * - * @return Current string - */ - String getString(); - - /** - * Sets the value of the current string. - * - * @param string The value to be set - */ - void setString(String string); - - /** - * Returns the number of words in the current string - * - * @return Number of words in the current string - */ - int count(); - - /** - * Returns a string that consists of all characters in the original string except for the characters - * in positions n, 2n, 3n, and so on, either deleting those or replacing them - * with a white space. The characters in the resulting string should be in the same order - * and with the same case as in the current string. - * - * - * Examples: - * - For n=2 and maintainSpacing=false, the method would return the string without the 2nd, 4th, - * 6th, and so on characters in the string. - * - For n=3 and maintainSpacing=true, the method would return the string with a space replacing - * the 3nd, 6th, 9th, and so on characters in the string. - * - * Values n and maintainSpacing are passed as parameters. The starting character is considered to be in Position 1. - * Special cases expected behavior - * - * throws IndexOutOfBoundsException If n is greater than the string length. - * throws IllegalArgumentException If "n" less than or equal to zero. - * - * @param n Determines the positions of the characters to be returned - * @param maintainSpacing Determines whether replace the missing characters with a space, in order - * to maintain the length of the original string. - * @return String made of characters at positions other than n, 2n, and so on in the current string - * - * - */ - String removeNthCharacter(int n, boolean maintainSpacing); - - /** - * Returns the words from position "startWord" to position "endWord" - * in the sentence, with 1 being the first Word in the String - * - * @param startWord - * Position of the first word to return - * @param endWord - * Position of the last word to return - * @return - * String array of the words from position "startWord" to position "endWord" - * Special cases - * throws IllegalArgumentException - * If either "startWord" or "endWord" are invalid (i.e., - * "startWord" <= 0, "endWord" <= 0, or "startWord" - * > "endWord") - * throws IndexOutOfBoundsException - * If the string has less than "endWord" words in it - */ - String[] getSubStrings(int startWord, int endWord); - - /** - * Given a string s and an integer array indices of the same length. - * 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] - * Output: "TestUnit" - * Explanation: - * indices: 4 5 6 7 0 1 2 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 - * 0 1 2 3 4 5 6 7 - * As shown, "UnitTest" becomes "TestUnit" after shuffling. - * - * special cases and assumptions - * s contains lower-case or upper-case English letters. - * All values of indices are unique (i.e. indices is a permutation of the integers from 0 to n - 1). - * 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 - * - * @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. - * - * @return Return the shuffled string. - * - * */ - String restoreString(int[] indices); - -} diff --git a/src/src/main/java/StringManipulation.java b/src/src/main/java/StringManipulation.java new file mode 100644 index 0000000..60b7190 --- /dev/null +++ b/src/src/main/java/StringManipulation.java @@ -0,0 +1,66 @@ +import java.util.*; + +public class StringManipulation implements StringManipulationInterface { + private String s; + + @Override + public void setString(String s) { + this.s = s; + } + + @Override + public int count() { + if (s == null || s.length() == 0) { + return 0; + } + return s.trim().split("\\s+").length; + } + + @Override + public String removeNthCharacter(int n, boolean includeSpaces) { + StringBuilder result = new StringBuilder(); + + for (int i = 0; i < s.length(); i++) { + char c = s.charAt(i); + + if ((includeSpaces && c != ' ') || !includeSpaces) { + if ((i + 1) % n != 0) { + result.append(c); + } else if (includeSpaces) { + result.append(' '); + } + } else { + result.append(c); + } + } + + return result.toString(); + } + + + @Override + public String[] getSubStrings(int start, int end) { + if (s == null || s.length() == 0) { + return new String[0]; + } + String[] words = s.split("\\s+"); + if (start < 0 || end > words.length || start > end) { + return new String[0]; + } + return Arrays.copyOfRange(words, start, end+1); + } + + @Override + public String restoreString(int[] array) { + if (s == null || array == null) { + return s; + } + char[] arr = new char[s.length()]; + for (int i = 0; i < array.length; i++) { + if (array[i] < arr.length) { + arr[array[i]] = s.charAt(i); + } + } + return new String(arr); + } +} diff --git a/src/src/main/java/StringManipulationInterface.java b/src/src/main/java/StringManipulationInterface.java new file mode 100644 index 0000000..1dd4775 --- /dev/null +++ b/src/src/main/java/StringManipulationInterface.java @@ -0,0 +1,16 @@ +public interface StringManipulationInterface { + // Method to set the string + void setString(String s); + + // Method to count the number of words in the string + int count(); + + // Method to remove the nth character from the string, considering spaces as well or not + String removeNthCharacter(int n, boolean considerSpaces); + + // Method to get an array of substrings of the string, from start to end (both inclusive) + String[] getSubStrings(int start, int end); + + // Method to restore the string by the provided array + String restoreString(int[] array); +} \ No newline at end of file diff --git a/src/src/test/java/StringManipulationTest.java b/src/src/test/java/StringManipulationTest.java new file mode 100644 index 0000000..f9fa1e4 --- /dev/null +++ b/src/src/test/java/StringManipulationTest.java @@ -0,0 +1,183 @@ +import org.junit.jupiter.api.AfterEach; +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; + + @BeforeEach + public void setUp() { + manipulatedstring = new StringManipulation(); + } + + @AfterEach + public void tearDown() { + manipulatedstring = null; + } + + @Test + public void testCount1() { + manipulatedstring.setString("This is my string"); + int length = manipulatedstring.count(); + assertEquals(4, length); + } + + @Test + public void testCount2() { + manipulatedstring.setString("One more string for testing"); + int length = manipulatedstring.count(); + assertEquals(5, length); + } + + @Test + public void testCount3() { + manipulatedstring.setString(""); + int length = manipulatedstring.count(); + assertEquals(0, length); + } + + @Test + public void testCount4() { + 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)); + } + + @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)); + } + + @Test + public void testRemoveNthCharacter3() { + manipulatedstring.setString(""); + assertEquals("", manipulatedstring.removeNthCharacter(2, false)); + } + + @Test + public void testRemoveNthCharacter4() { + manipulatedstring.setString("111111111"); + assertEquals("1 1 1 1 1", manipulatedstring.removeNthCharacter(2, true)); + } + + @Test + public void testRemoveNthCharacter5() { + manipulatedstring.setString("111111111"); + assertEquals("11111", manipulatedstring.removeNthCharacter(2, false)); + } + + @Test + public void testRemoveNthCharacter6() { + manipulatedstring.setString("1"); + assertEquals("1", manipulatedstring.removeNthCharacter(4, true)); + } + + @Test + public void testRemoveNthCharacter7() { + manipulatedstring.setString("123"); + assertEquals("12", manipulatedstring.removeNthCharacter(3, false)); + } + + @Test + public void testGeSubStrings1() { + manipulatedstring.setString("This is my string"); + String [] sStings = manipulatedstring.getSubStrings(3, 4); + assertEquals(sStings[0], "my"); + assertEquals(sStings[1], "string"); + } + + @Test + public void testGeSubStrings2() { + manipulatedstring.setString("One more test string for testing"); + String [] sStings = manipulatedstring.getSubStrings(2, 3); + assertEquals(sStings[0], "test"); + assertEquals(sStings[1], "string"); + } + + @Test + public void testGeSubStrings3() { + manipulatedstring.setString("One more test string for testing"); + String [] sStings = manipulatedstring.getSubStrings(0, 4); + assertEquals(sStings[0], "One"); + assertEquals(sStings[1], "more"); + assertEquals(sStings[2], "test"); + assertEquals(sStings[3], "string"); + } + + @Test + public void testGeSubStrings4() { + manipulatedstring.setString("Only one word"); + String [] sStings = manipulatedstring.getSubStrings(0, 1); + assertEquals(sStings[0], "Only"); + } + + @Test + public void testGeSubStrings5() { + manipulatedstring.setString("Two words only"); + String [] sStings = manipulatedstring.getSubStrings(1, 1); + assertEquals(sStings[0], "words"); + } + + @Test + public void testGeSubStrings6() { + manipulatedstring.setString("Three words here"); + String [] sStings = manipulatedstring.getSubStrings(0, 3); + assertEquals(sStings[0], "Three"); + assertEquals(sStings[1], "words"); + assertEquals(sStings[2], "here"); + } + + @Test + public void testRestoreString1() + { + manipulatedstring.setString("abc"); + int [] array = {2,1,0}; + String restoreString = manipulatedstring.restoreString(array); + assertEquals("cba", restoreString); + } + + @Test + public void testRestoreString2() + { + manipulatedstring.setString("abcdef"); + int [] array = {5,4,3,2,1,0}; + String restoreString = manipulatedstring.restoreString(array); + assertEquals("fedcba", restoreString); + } + + @Test + public void testRestoreString3() + { + manipulatedstring.setString(""); + int [] array = {}; + String restoreString = manipulatedstring.restoreString(array); + assertEquals("", restoreString); + } + + @Test + public void testRestoreString4() + { + manipulatedstring.setString("a"); + int [] array = {0}; + String restoreString = manipulatedstring.restoreString(array); + assertEquals("a", restoreString); + } + + @Test + public void testRestoreString5() + { + manipulatedstring.setString("aaabaaa"); + int [] array = {6,5,4,3,2,1,0}; + String restoreString = manipulatedstring.restoreString(array); + assertEquals("aaabaaa", restoreString); + } +} diff --git a/src/test/java/StringManipulationTest.java b/src/test/java/StringManipulationTest.java deleted file mode 100644 index 6692c2c..0000000 --- a/src/test/java/StringManipulationTest.java +++ /dev/null @@ -1,149 +0,0 @@ -import org.junit.jupiter.api.AfterEach; -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; - - @BeforeEach - public void setUp() { - manipulatedstring = new StringManipulation(); - } - - @AfterEach - public void tearDown() { - manipulatedstring = null; - } - - @Test - public void testCount1() { - manipulatedstring.setString("This is my string"); - int length = manipulatedstring.count(); - assertEquals(4, length); - } - - @Test - public void testCount2() { - fail("Not yet implemented"); - } - - @Test - public void testCount3() { - fail("Not yet implemented"); - } - - @Test - public void testCount4() { - fail("Not yet implemented"); - } - - @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)); - } - - @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)); - } - - @Test - public void testRemoveNthCharacter3() { - fail("Not yet implemented"); - } - - @Test - public void testRemoveNthCharacter4() { - fail("Not yet implemented"); - } - - @Test - public void testRemoveNthCharacter5() { - fail("Not yet implemented"); - } - - @Test - public void testRemoveNthCharacter6() { - fail("Not yet implemented"); - } - - @Test - public void testRemoveNthCharacter7() { - fail("Not yet implemented"); - } - - @Test - public void testGeSubStrings1() { - manipulatedstring.setString("This is my string"); - String [] sStings = manipulatedstring.getSubStrings(3, 4); - - assertEquals(sStings[0], "my"); - assertEquals(sStings[1], "string"); - } - - @Test - public void testGeSubStrings2() { - fail("Not yet implemented"); - } - @Test - public void testGeSubStrings3() { - fail("Not yet implemented"); - } - @Test - public void testGeSubStrings4() { - fail("Not yet implemented"); - } - @Test - public void testGeSubStrings5() { - fail("Not yet implemented"); - } - @Test - public void testGeSubStrings6() { - fail("Not yet implemented"); - } - - @Test - public void testRestoreString1() - { - manipulatedstring.setString("art"); - int [] array; - array=new int[]{1,0,2}; - String restoreString = manipulatedstring.restoreString(array); - assertEquals(restoreString, "rat"); - } - - @Test - public void testRestoreString2() - { - fail("Not yet implemented"); - - } - - @Test - public void testRestoreString3() - { - fail("Not yet implemented"); - - } - - @Test - public void testRestoreString4() - { - fail("Not yet implemented"); - - } - - @Test - public void testRestoreString5() - { - fail("Not yet implemented"); - - } - -}