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
107 changes: 95 additions & 12 deletions src/main/java/StringManipulation.java
Original file line number Diff line number Diff line change
@@ -1,33 +1,116 @@
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
/**
* Counts the number of non-empty words in the string.
* @return The count of words.
*/
public int count() {
return 0;
String[] words = string.split(" ");
int count = 0;

for (String word : words) {
if (!word.isEmpty()) {
count++;
}
}

return count;
}

/**
* Removes the character at the nth position in the string.
* @param n The position of the character to be removed.
* @param maintainSpacing Determines whether to maintain spacing when removing characters.
* @return The string with the nth characters removed.
*/
@Override
public String removeNthCharacter(int n, boolean maintainSpacing) {
return null;
}
if (n <= 0) {
throw new IllegalArgumentException();
} else if (n > string.length()) {
throw new IndexOutOfBoundsException();
}

@Override
public String[] getSubStrings(int startWord, int endWord) {
return null;
StringBuilder stringBuilder = new StringBuilder();

for (int i = 0; i < string.length(); i++) {
if ((i + 1) % n == 0) {
if (maintainSpacing) {
stringBuilder.append(" ");
}
} else {
stringBuilder.append(string.charAt(i));
}
}

return stringBuilder.toString();
}

/**
* Retrieves a substring of words from the specified startWord to the endWord.
* @param startWord The starting index of the substring.
* @param endWord The ending index of the substring.
* @return An array of substrings.
*/
@Override
public String restoreString(int[] indices) {
return null;
public String[] getSubStrings(int startWord, int endWord){
if (startWord <= 0 || endWord <= 0 || startWord > endWord) {
throw new IllegalArgumentException();
}

String[] preFilteredResult = string.split(" ");
String[] substrings = new String[endWord - startWord + 1];
int wordCount = 1;
int substringsIndex = 0;

for (String result : preFilteredResult) {
if (!result.isEmpty()) {
if (wordCount >= startWord && wordCount <= endWord) {
substrings[substringsIndex] = result;
substringsIndex++;
}
wordCount++;
}
}

if (wordCount < endWord) {
throw new IndexOutOfBoundsException();
}

return substrings;
}

/**
* Restores the characters from the given indices to form a string.
* @param indices The indices of the characters to be restored.
* @return The restored string.
*/
@Override
public String restoreString(int[] indices){
if (indices.length != string.length()) {
throw new IllegalArgumentException();
}

String[] restoredStringArray = new String[indices.length];

for (int i = 0; i < indices.length; i++) {
int oldIndex = indices[i];
if (oldIndex < 0 || oldIndex >= string.length()) {
throw new IndexOutOfBoundsException();
}
restoredStringArray[i] = String.valueOf(string.charAt(oldIndex));
}

return String.join("", restoredStringArray);
Comment on lines +103 to +113

Choose a reason for hiding this comment

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

Your restoreString() basically has the same logic as mine and there is nothing wrong with your code:)
Just to share, instead of an array, a StringBuilder could be used as an alternative, which I used in my code. The same operation can be performed with append() for the StringBuilder, then return the string representation by using toString() on the StringBuilder.

}

}
179 changes: 119 additions & 60 deletions src/test/java/StringManipulationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,37 @@ public void testCount1() {
assertEquals(4, length);
}

// Checks what happens if an empty string is passed in
@Test
public void testCount2() {
fail("Not yet implemented");
manipulatedstring.setString(" ");
int length = manipulatedstring.count();
assertEquals(0, length);
}

// This test checks that numbers are recognized as strings
@Test
public void testCount3() {
fail("Not yet implemented");
manipulatedstring.setString("132 444");
int length = manipulatedstring.count();
assertEquals(2, length);
}

// This test ensures that the counter works on a long string
@Test
public void testCount4() {
fail("Not yet implemented");
String phrase = "the quick brown fox jumps over the lazy dog";
int expectedCount = 9;

StringManipulation stringManipulation = new StringManipulation();
stringManipulation.setString(phrase);

int actualCount = stringManipulation.count();

assertEquals(expectedCount, actualCount);
}


@Test
public void testRemoveNthCharacter1() {
manipulatedstring.setString("I'd b3tt3r put s0me d161ts in this 5tr1n6, right?");
Expand All @@ -52,61 +68,88 @@ 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");
manipulatedstring.setString("I'd b3tt3r put s0me d161ts in this 5tr1n6, right?");
assertThrows(IllegalArgumentException.class, () -> manipulatedstring.removeNthCharacter(0, true));
}

@Test
public void testRemoveNthCharacter4() {
fail("Not yet implemented");
manipulatedstring.setString("I'd b3tt3r put s0me d161ts in this 5tr1n6, right?");
assertEquals(" ", manipulatedstring.removeNthCharacter(1, true));
}

@Test
public void testRemoveNthCharacter5() {
fail("Not yet implemented");
manipulatedstring.setString("I'd b3tt3r put s0me d161ts in this 5tr1n6, right?");
assertEquals("", manipulatedstring.removeNthCharacter(1, false));
}

@Test
public void testRemoveNthCharacter6() {
fail("Not yet implemented");
manipulatedstring.setString("I'd b3tt3r put s0me d161ts in this 5tr1n6, right?");
assertThrows(IndexOutOfBoundsException.class, () -> manipulatedstring.removeNthCharacter(100, false));
}


// Checks removing a negative Nth character
@Test
public void testRemoveNthCharacter7() {
fail("Not yet implemented");
manipulatedstring.setString("I'd b3tt3r put s0me d161ts in this 5tr1n6, right?");
assertThrows(IllegalArgumentException.class, () -> manipulatedstring.removeNthCharacter(-1, false));
}

@Test
public void testGeSubStrings1() {
public void testGetSubStrings1() {
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");
}
// START
// Test getting substrings with a starting value less than 0
@Test
public void testGetSubStrings2() {
manipulatedstring.setString("This is my string");
// This test tries to get substrings with a negative starting value
assertThrows(IllegalArgumentException.class, () -> manipulatedstring.getSubStrings(-1, 4));
}

@Test
public void testGetSubStrings3() {
manipulatedstring.setString("This is my string");
// This test tries to get substrings with the starting value greater than the ending one
assertThrows(IllegalArgumentException.class, () -> manipulatedstring.getSubStrings(2, 1));
}

@Test
public void testGetSubStrings4() {
manipulatedstring.setString("This is my string");
// This test tries to get substrings with both the starting and ending value negative
assertThrows(IllegalArgumentException.class, () -> manipulatedstring.getSubStrings(-2, -1));
}

@Test
public void testGetSubStrings5() {
manipulatedstring.setString("This is my string");
assertThrows(IndexOutOfBoundsException.class, () -> manipulatedstring.getSubStrings(1, 100));
}

@Test
public void testGetSubStrings6() {
manipulatedstring.setString("This is my string");
String [] sStings = manipulatedstring.getSubStrings(1, 1);

assertEquals(sStings.length, 1);
assertEquals(sStings[0], "This");
}



// END

@Test
public void testRestoreString1()
Expand All @@ -118,32 +161,48 @@ public void testRestoreString1()
assertEquals(restoreString, "rat");
}

// Tests that case is maintained
@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");

}

}
manipulatedstring.setString("AaBbCc");
int [] array;
array=new int[]{1,0,3,2,5,4};
String restoreString = manipulatedstring.restoreString(array);
assertEquals(restoreString, "aAbBcC");
}


@Test
public void testRestoreString3() {
manipulatedstring.setString("AaBbCc");
int[] indices = {1};
// Tests when the indices array size is smaller than the string length
assertThrows(IllegalArgumentException.class, () -> manipulatedstring.restoreString(indices));
}

@Test
public void testRestoreString4() {
manipulatedstring.setString("AaBbCc");
int[] indices = {1, 2, 3, 4, 5, 6, 7};
// Tests when the indices array size is larger than the string length
assertThrows(IllegalArgumentException.class, () -> manipulatedstring.restoreString(indices));
}

@Test
public void testRestoreString5() {
manipulatedstring.setString("AaBbCc");
int[] indices = {1, 2, 3, 4, 5, -1};
// Tests when an index in the indices array is less than zero
assertThrows(IndexOutOfBoundsException.class, () -> manipulatedstring.restoreString(indices));
}

@Test
public void testRestoreString6() {
manipulatedstring.setString("AaBbCc");
int[] indices = {1, 2, 3, 4, 5, 7};
// Tests when an index in the indices array is larger than the string length
assertThrows(IndexOutOfBoundsException.class, () -> manipulatedstring.restoreString(indices));
}

}