Skip to content
Open
99 changes: 93 additions & 6 deletions src/main/java/StringManipulation.java
Original file line number Diff line number Diff line change
@@ -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()){
Comment thread
Hester1937 marked this conversation as resolved.
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++) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For this method rather than iterating through and copying one word at a time, you can use the arraycopy method to copy the array, which is more efficient. Picture for reference.
image

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you Jonathan

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);
}

}

6 changes: 3 additions & 3 deletions src/main/java/StringManipulationInterface.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down
140 changes: 99 additions & 41 deletions src/test/java/StringManipulationTest.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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");
Expand All @@ -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()
{
Expand All @@ -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);
});
}

}