diff --git a/src/main/java/StringManipulation.java b/src/main/java/StringManipulation.java index 4deb6f1..91ec691 100644 --- a/src/main/java/StringManipulation.java +++ b/src/main/java/StringManipulation.java @@ -1,33 +1,125 @@ public class StringManipulation implements StringManipulationInterface { + private String str; - @Override - public String getString() { - return null; - } + @Override + public String getString() { + return this.str; + } - @Override - public void setString(String string) { - } + @Override + public void setString(String string) { + this.str = string; + } - @Override - public int count() { - return 0; - } + @Override + public int count() { + if (str == null || str.isEmpty()) { + return 0; + } + + String[] splitStr = str.split("\\s+"); + return splitStr.length; + /* + * int wordCount = 0; + boolean whitespace = false; - @Override - public String removeNthCharacter(int n, boolean maintainSpacing) { - return null; - } + // Iterate through each character in the string + for (int i = 0; i < str.length(); i++) { + char c = str.charAt(i); - @Override - public String[] getSubStrings(int startWord, int endWord) { - return null; - } + // Check if the character is a whitespace + if (Character.isWhitespace(c)) { + whitespace = true; + + // Check if the character is a letter or digit + if (Character.isLetterOrDigit(str.charAt(i+1))) { + // If we are not inside a word, increment the word count + if (whitespace) { + wordCount++; + whitespace = false; + } + }*/ + } - @Override - public String restoreString(int[] indices) { - return null; - } + @Override + public String removeNthCharacter(int n, boolean maintainSpacing) { + if (n <= 0) { + throw new IllegalArgumentException("'n' must be greater than zero."); + } + if (n > str.length()) { + throw new IndexOutOfBoundsException("'n' is greater than the string length."); + } + + String result = new String(); + result = result.concat(str.charAt(0) + ""); + for (int i = 1; i < str.length(); i++) { + if((i+1) % n != 0) { + result = result.concat(str.charAt(i) + ""); + } + else { + if (maintainSpacing) { + result = result.concat(" "); + } + } + } + + return result; + } + + @Override + public String[] getSubStrings(int startWord, int endWord) { + // Check for invalid arguments + if (startWord <= 0 || endWord <= 0 || startWord > endWord) { + throw new IllegalArgumentException("Invalid startWord or endWord"); + } + + // Split the sentence into words + String[] words = str.split("\\s+"); + + // Check if the sentence has enough words + if (endWord > words.length) { + throw new IndexOutOfBoundsException("The String has less than " + endWord + " words"); + } + + // Calculate the number of words to put in the array + int wordCount = endWord - startWord + 1; + + // Create a new array to store the extracted words + String[] subStrArr = new String[wordCount]; + + // Extract the words from the specified positions + for (int i = 0; i < wordCount; i++) { + subStrArr[i] = words[startWord - 1 + i]; + } + + return subStrArr; + } + + @Override + public String restoreString(int[] indices) { + //throws IllegalArgumentException if not s.length == indices.length == n + // throws IndexOutOfBoundsException if indices[i]< 0 or indices[i]> string length + if(str.length() != indices.length) { + throw new IllegalArgumentException("The indices array must be the same length as the string."); + } + char[] temp = str.toCharArray(); + char[] shuffled = new char[str.length()]; + String output = new String(); + for(int i = 0; i < str.length(); i++) { + if(i < indices.length) { + int pos = indices[i]; + if(pos < 0 || pos > str.length()) { + throw new IndexOutOfBoundsException("All values in the indices array must correspond to valid indexes in the string."); + } + shuffled[pos] = temp[i]; + } + else{ + shuffled[i] = temp[i]; + } + } + output = String.valueOf(shuffled); + return output; + } } 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..c218b0e 100644 --- a/src/test/java/StringManipulationTest.java +++ b/src/test/java/StringManipulationTest.java @@ -28,58 +28,89 @@ public void testCount1() { @Test public void testCount2() { - fail("Not yet implemented"); + manipulatedstring.setString("Thisismystring"); + int length = manipulatedstring.count(); + assertEquals(1, length); } @Test public void testCount3() { - fail("Not yet implemented"); + manipulatedstring.setString("23 and me"); + int length = manipulatedstring.count(); + assertEquals(3, length); } @Test - public void testCount4() { - fail("Not yet implemented"); + public void testCount4() { + manipulatedstring.setString("This is my string"); + int length = manipulatedstring.count(); + assertEquals(4, length); + } + + @Test + public void testCount5() { + manipulatedstring.setString(""); + int length = manipulatedstring.count(); + assertEquals(0, 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)); + String temp = manipulatedstring.removeNthCharacter(3, false); + assertEquals("I' bttr uts0e 16tsinths trn6 rgh?", temp); } @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)); + String temp = manipulatedstring.removeNthCharacter(3, true); + assertEquals("I' b tt r ut s0 e 16 ts in th s tr n6 r gh ?", temp); } @Test public void testRemoveNthCharacter3() { - fail("Not yet implemented"); + manipulatedstring.setString("I'd b3tt3r put s0me d161ts in this 5tr1n6, right?"); + String temp = manipulatedstring.removeNthCharacter(5, false); + + assertEquals("I'd 3tt3 puts0med161s inthis5tr16, rght?", temp); } @Test public void testRemoveNthCharacter4() { - fail("Not yet implemented"); + manipulatedstring.setString("I'd b3tt3r put s0me d161ts in this 5tr1n6, right?"); + assertEquals("I'd 3tt3 put s0me d161 s in this 5tr1 6, r ght?", manipulatedstring.removeNthCharacter(5, true)); } @Test public void testRemoveNthCharacter5() { - fail("Not yet implemented"); + manipulatedstring.setString("I'd b3tt3r put s0me d161ts in this 5tr1n6, right?"); + Throwable exception = assertThrows(IndexOutOfBoundsException.class, () -> manipulatedstring.removeNthCharacter(100, false)); + assertEquals("'n' is greater than the string length.", exception.getMessage()); } @Test public void testRemoveNthCharacter6() { - fail("Not yet implemented"); + manipulatedstring.setString("I'd b3tt3r put s0me d161ts in this 5tr1n6, right?"); + Throwable exception = assertThrows(IllegalArgumentException.class, () -> manipulatedstring.removeNthCharacter(0, false)); + assertEquals("'n' must be greater than zero.", exception.getMessage()); } - + @Test public void testRemoveNthCharacter7() { - fail("Not yet implemented"); + manipulatedstring.setString("I'd b3tt3r put s0me d161ts in this 5tr1n6, right?"); + Throwable exception = assertThrows(IllegalArgumentException.class, () -> manipulatedstring.removeNthCharacter(-1, false)); + assertEquals("'n' must be greater than zero.", exception.getMessage()); } @Test - public void testGeSubStrings1() { + public void testRemoveNthCharacter8() { + manipulatedstring.setString("I'd b3tt3r put s0me d161ts in this 5tr1n6, right?"); + assertEquals("I'd b3tt3r put s0me d161ts in this 5tr1n6, right", manipulatedstring.removeNthCharacter(49, false)); + } + + @Test + public void testGetSubStrings1() { manipulatedstring.setString("This is my string"); String [] sStings = manipulatedstring.getSubStrings(3, 4); @@ -88,24 +119,49 @@ public void testGeSubStrings1() { } @Test - public void testGeSubStrings2() { - fail("Not yet implemented"); + public void testGetSubStrings2() { + manipulatedstring.setString("Thisismystring"); + Throwable exception = assertThrows(Exception.class, () -> manipulatedstring.getSubStrings(3, 4)); + assertEquals("The String has less than 4 words", exception.getMessage()); } @Test - public void testGeSubStrings3() { - fail("Not yet implemented"); + public void testGetSubStrings3() { + manipulatedstring.setString(""); + Throwable exception = assertThrows(Exception.class, () -> manipulatedstring.getSubStrings(1, 4)); + assertEquals("The String has less than 4 words", exception.getMessage()); } @Test - public void testGeSubStrings4() { - fail("Not yet implemented"); + public void testGetSubStrings4() { + manipulatedstring.setString("This is my string"); + String [] sStings = manipulatedstring.getSubStrings(3, 3); + + assertEquals(sStings[0], "my"); } @Test - public void testGeSubStrings5() { - fail("Not yet implemented"); + public void testGetSubStrings5() { + manipulatedstring.setString("This is my string"); + Throwable exception = assertThrows(IllegalArgumentException.class, () -> manipulatedstring.getSubStrings(5, 1)); + assertEquals("Invalid startWord or endWord", exception.getMessage()); } @Test - public void testGeSubStrings6() { - fail("Not yet implemented"); + public void testGetSubStrings6() { + manipulatedstring.setString("This is my string"); + Throwable exception = assertThrows(IndexOutOfBoundsException.class, () -> manipulatedstring.getSubStrings(1, 100)); + assertEquals("The String has less than 100 words", exception.getMessage()); + } + + @Test + public void testGetSubStrings7() { + manipulatedstring.setString("This is my string"); + Throwable exception = assertThrows(IllegalArgumentException.class, () -> manipulatedstring.getSubStrings(-1, 3)); + assertEquals("Invalid startWord or endWord", exception.getMessage()); + } + + @Test + public void testGetSubStrings8() { + manipulatedstring.setString("This is my string"); + Throwable exception = assertThrows(IllegalArgumentException.class, () -> manipulatedstring.getSubStrings(1, -3)); + assertEquals("Invalid startWord or endWord", exception.getMessage()); } @Test @@ -121,28 +177,44 @@ public void testRestoreString1() @Test public void testRestoreString2() { - fail("Not yet implemented"); - + manipulatedstring.setString("This is my string"); + + int [] array; + array=new int[]{1,0,2,5,3,4,7,6}; + Throwable exception = assertThrows(IllegalArgumentException.class, () -> manipulatedstring.restoreString(array) ); + assertEquals("The indices array must be the same length as the string.", exception.getMessage()); } @Test public void testRestoreString3() { - fail("Not yet implemented"); - + manipulatedstring.setString("This is my string"); + int [] array; + array=new int[]{-1,0,2,5,3,4,7,6,13,15,16,9,10,12,8,14,11}; + Throwable exception = assertThrows(IndexOutOfBoundsException.class, () -> manipulatedstring.restoreString(array) ); + assertEquals("All values in the indices array must correspond to valid indexes in the string.", exception.getMessage()); } @Test public void testRestoreString4() { - fail("Not yet implemented"); + manipulatedstring.setString("This is my string"); + int [] array; + array=new int[]{1,0,2,5,3,4,7,6,13,15,16,9,10,12,8,100,11}; + Throwable exception = assertThrows(IndexOutOfBoundsException.class, () -> manipulatedstring.restoreString(array) ); + assertEquals("All values in the indices array must correspond to valid indexes in the string.", exception.getMessage()); + } @Test public void testRestoreString5() { - fail("Not yet implemented"); + manipulatedstring.setString("second test "); + int [] array; + array=new int[]{11,10,9,8,7,6,5,4,3,2,1,0}; + String restoreString = manipulatedstring.restoreString(array); + assertEquals(restoreString, " tset dnoces"); }