Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 59 additions & 14 deletions src/main/java/StringManipulation.java
Original file line number Diff line number Diff line change
@@ -1,33 +1,78 @@
public class StringManipulation implements StringManipulationInterface {
public class StringManipulation {

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 (string == null || string.isEmpty()) {
return 0;
}
String[] words = string.trim().split("\\s+");
return words.length;
}

@Override
public String removeNthCharacter(int n, boolean maintainSpacing) {
return null;
if (n <= 0) {
throw new IllegalArgumentException("Invalid value for n");
}
if (string == null || string.isEmpty() || n > string.length()) {
throw new IndexOutOfBoundsException("n is greater than the string length");
}

StringBuilder result = new StringBuilder();
for (int i = 0; i < string.length(); i++) {
if ((i + 1) % n != 0) {
result.append(string.charAt(i));
} else if (maintainSpacing) {
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 values");
}
if (string == null || string.isEmpty()) {
throw new IndexOutOfBoundsException("String is empty");
}

@Override
public String restoreString(int[] indices) {
return null;
String[] words = string.trim().split("\\s+");
if (endWord > words.length) {
throw new IndexOutOfBoundsException("String has fewer words than endWord");
}

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

public String restoreString(int[] indices) {
if (string == null || indices == null || string.length() != indices.length) {

Choose a reason for hiding this comment

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

Java already throws a runtime error if you try to call restoreString() with an uninitialized int array, so I don't think you need the second condition here.

throw new IllegalArgumentException("Invalid input: string length does not match indices length");
}

char[] charArray = string.toCharArray();
char[] restoredArray = new char[charArray.length];
for (int i = 0; i < charArray.length; i++) {
int index = indices[i];
if (index < 0 || index >= charArray.length) {
throw new IndexOutOfBoundsException("Invalid index value");
}
restoredArray[index] = charArray[i];
}
return new String(restoredArray);
}
}
150 changes: 103 additions & 47 deletions src/test/java/StringManipulationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;


import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.fail;

public class StringManipulationTest {

private StringManipulationInterface manipulatedstring;
private StringManipulation manipulatedstring;

@BeforeEach
public void setUp() {
Expand All @@ -28,19 +30,28 @@ public void testCount1() {

@Test
public void testCount2() {
fail("Not yet implemented");
manipulatedstring.setString("This is another string");
int count = manipulatedstring.count();
assertEquals(4, count);
}


@Test
public void testCount3() {
fail("Not yet implemented");
manipulatedstring.setString("Count multiple words");
int count = manipulatedstring.count();
assertEquals(3, count);
}


@Test
public void testCount4() {
fail("Not yet implemented");
manipulatedstring.setString("");
int count = manipulatedstring.count();
assertEquals(0, count);
}


@Test
public void testRemoveNthCharacter1() {
manipulatedstring.setString("I'd b3tt3r put s0me d161ts in this 5tr1n6, right?");
Expand All @@ -55,95 +66,140 @@ public void testRemoveNthCharacter2() {

@Test
public void testRemoveNthCharacter3() {

Choose a reason for hiding this comment

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

The following test cases (testRemoveNthCharacter 3-7) are all variations of the first two test cases. Please revise these test cases so that they're testing for different kinds of input (e.g., n <= 0, string.isEmpty(), etc.)

fail("Not yet implemented");
manipulatedstring.setString("Hello, World!");
String result = manipulatedstring.removeNthCharacter(2, false);
assertEquals("Hlo ol!", result);
}


@Test
public void testRemoveNthCharacter4() {
fail("Not yet implemented");
manipulatedstring.setString("Hello, World!");
String result = manipulatedstring.removeNthCharacter(5, true);
assertEquals("Hell , Wo ld!", result);
}


@Test
public void testRemoveNthCharacter5() {
fail("Not yet implemented");
manipulatedstring.setString("Hello, World!");
assertThrows(IllegalArgumentException.class, () -> {
manipulatedstring.removeNthCharacter(0, false);
});
}



@Test
public void testRemoveNthCharacter6() {
fail("Not yet implemented");
manipulatedstring.setString("Hello, World!");
String result = manipulatedstring.removeNthCharacter(3, false);
assertEquals("Helo Wrl!", result);
}


@Test
public void testRemoveNthCharacter7() {
fail("Not yet implemented");
manipulatedstring.setString("world");
String result = manipulatedstring.removeNthCharacter(2, true);
assertEquals("w r d", result);
}


@Test
public void testGeSubStrings1() {
public void testGetSubStrings1() {

Choose a reason for hiding this comment

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

You need to add unit tests for all the if statements in getSubstrings() in StringManipulation.java
ex:

if (startWord <= 0 || endWord <= 0 || startWord > endWord) {
    throw new IllegalArgumentException("Invalid startWord or endWord values");
}
if (string == null || string.isEmpty()) {
    throw new IndexOutOfBoundsException("String is empty");
}
String[] words = string.trim().split("\\s+");
if (endWord > words.length) {
    throw new IndexOutOfBoundsException("String has fewer words than endWord");
}

manipulatedstring.setString("This is my string");
String [] sStings = manipulatedstring.getSubStrings(3, 4);

assertEquals(sStings[0], "my");
assertEquals(sStings[1], "string");
String[] substrings = manipulatedstring.getSubStrings(3, 4);
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");
String[] result = manipulatedstring.getSubStrings(2, 4);
assertArrayEquals(new String[]{"is", "a", "test"}, result);
}


@Test
public void testGeSubStrings3() {
fail("Not yet implemented");
public void testGetSubStrings3() {
manipulatedstring.setString("Hello, World!");
String[] result = manipulatedstring.getSubStrings(1, 2);
assertArrayEquals(new String[]{"Hello,", "World!"}, result);
}


@Test
public void testGeSubStrings4() {
fail("Not yet implemented");
public void testGetSubStrings4() {
manipulatedstring.setString("mochi is the best dog");
String[] result = manipulatedstring.getSubStrings(2, 4);
assertArrayEquals(new String[]{"is", "the", "best"}, result);
}




@Test
public void testGeSubStrings5() {
fail("Not yet implemented");
public void testGetSubStrings5() {
manipulatedstring.setString("This is a test");
assertThrows(IndexOutOfBoundsException.class, () -> {
manipulatedstring.getSubStrings(4, 6);
});
}



@Test
public void testGeSubStrings6() {
fail("Not yet implemented");
public void testGetSubStrings6() {
manipulatedstring.setString("This is a test");
String[] result = manipulatedstring.getSubStrings(1, 3);
assertArrayEquals(new String[]{"This", "is", "a"}, result);
}


@Test
public void testRestoreString1()
{
public void testRestoreString1() {

Choose a reason for hiding this comment

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

Need more unique test cases for testing restoreString() (e.g., string.length !== array.length, any of the indices in int[] array are negative or greater than or equal to the length of the string).

manipulatedstring.setString("art");
int [] array;
array=new int[]{1,0,2};
int[] array = {1, 0, 2};
String restoreString = manipulatedstring.restoreString(array);
assertEquals(restoreString, "rat");
assertEquals("rat", restoreString);
}

@Test
public void testRestoreString2()
{
fail("Not yet implemented");

public void testRestoreString2() {
manipulatedstring.setString("opna");
int[] array = {3, 0, 2, 1};
String restoreString = manipulatedstring.restoreString(array);
assertEquals("pano", restoreString);
}

@Test
public void testRestoreString3()
{
fail("Not yet implemented");

@Test
public void testRestoreString3() {
manipulatedstring.setString("abcdefg");
int[] array = {6, 5, 4, 3, 2, 1, 0};
String restoreString = manipulatedstring.restoreString(array);
assertEquals("gfedcba", restoreString);
}

@Test
public void testRestoreString4()
{
fail("Not yet implemented");

@Test
public void testRestoreString4() {
manipulatedstring.setString("Hello");
int[] array = {0, 1, 2, 3, 4};
String restoreString = manipulatedstring.restoreString(array);
assertEquals("Hello", restoreString);
}

@Test
public void testRestoreString5()
{
fail("Not yet implemented");

@Test
public void testRestoreString5() {
manipulatedstring.setString("abc");
int[] array = {1, 2, 0};
String restoreString = manipulatedstring.restoreString(array);
assertEquals("cab", restoreString);
}


}