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
83 changes: 74 additions & 9 deletions src/main/java/StringManipulation.java
Original file line number Diff line number Diff line change
@@ -1,33 +1,98 @@
import java.util.Arrays;

// Description: This class implements various methods that manipulates strings.
public class StringManipulation implements StringManipulationInterface {

private String str = null;
@Override
public String getString() {
return null;
return this.str;
}

@Override
public void setString(String string) {
this.str = string;
}

@Override
public int count() {
return 0;
if(this.str == null || this.str.isEmpty()){
return 0;
}

String[] words = this.str.split("\\s+");

int wordCount = 0;
for (String word : words) {
// Count words and numerical values as a word.
// Ignore punctuation.
if (word.matches(".*[a-zA-Z0-9].*")) {
wordCount++;
}
}
return wordCount;
}

@Override
public String removeNthCharacter(int n, boolean maintainSpacing) {
return null;
if (n > str.length()) {
throw new IndexOutOfBoundsException("The argument is out of bound.");
} else if (n <= 0) {
throw new IllegalArgumentException("n index cannot be less than or equal to 0.");
}

StringBuilder stringBuilder = new StringBuilder(str);

// Loop through multiples of n
for (int i = 1; n * i <= str.length(); i++) {
int index = n * i - 1;
if (maintainSpacing) {
stringBuilder.setCharAt(index, ' ');
} else {
// Decrease index by i to account for previously deleted characters
stringBuilder.deleteCharAt(index - (i - 1));
}
}

return stringBuilder.toString();
}

@Override
public String[] getSubStrings(int startWord, int endWord) {
return null;
public String[] getSubStrings(int startWord, int endWord){
// Validate the inputs
if (startWord <= 0 || endWord <= 0 || startWord > endWord) {
throw new IllegalArgumentException("StartWord must be greater than 0 and less than or equal to EndWord.");
}

// Split the string into words
String[] words = this.str.trim().split("\\s");

// Validate that endWord does not exceed the number of words
if (endWord > words.length) {
throw new IndexOutOfBoundsException("The positions must be within the bounds");
}

// Return the subarray of words
return Arrays.copyOfRange(words, startWord - 1, endWord);
}

@Override
public String restoreString(int[] indices) {
return null;
}

// Check if the length of the string and indices array are the same
if (str.length() != indices.length) {
throw new IllegalArgumentException("Length of string and indices array must be equal.");
}

// Array of characters to rebuild the string.
char[] shuffledString = new char[str.length()];
for (int i = 0; i < indices.length; i++) {
int index = indices[i];
// Check if the given indice is within the bounds of the string
if (index < 0 || index >= str.length()) {
throw new IndexOutOfBoundsException("Index " + index + " is out of bounds.");
}
// Assign the character to the char array at the index position
shuffledString[index] = str.charAt(i);
}
return new String(shuffledString);
}
}
161 changes: 136 additions & 25 deletions src/test/java/StringManipulationTest.java
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;


import static org.junit.jupiter.api.Assertions.*;

public class StringManipulationTest {
Expand All @@ -19,63 +19,136 @@ public void tearDown() {
manipulatedstring = null;
}

@Test
public void testgetString(){
manipulatedstring.setString("Hello!");
String expected = "Hello!";
String actual = manipulatedstring.getString();
assertEquals(expected, actual);
}

@Test
public void testCount1() {
manipulatedstring.setString("This is my string");
int length = manipulatedstring.count();
assertEquals(4, length);
}

//Test when string has multiple punctuations.
@Test
public void testCount2() {
fail("Not yet implemented");
manipulatedstring.setString("This isn't my string!");
int length = manipulatedstring.count();
assertEquals(4, length);
}

// Test when string is empty.
// Expected return is 0
@DisplayName("Count empty string")
@Test
public void testCount3() {
fail("Not yet implemented");
manipulatedstring.setString("");
int length = manipulatedstring.count();
assertEquals(0, length);
}

// Test when string is null

Choose a reason for hiding this comment

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

I like that you added comments describing a bit about the test. Try looking into the @DisplayName annotation for an addition nice way to format test details

Copy link
Author

Choose a reason for hiding this comment

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

Oh, that's really cool. I'll use that

// Expected return is 0
@Test
@DisplayName("Count null")
public void testCount4() {
fail("Not yet implemented");
manipulatedstring.setString(null);
int length = manipulatedstring.count();
assertEquals(0, length);
}

// Test when string has only empty space character.
// Expected return is 0
@Test
public void testCount5() {
manipulatedstring.setString(" ");
int length = manipulatedstring.count();
assertEquals(0, length); }

// Test when string has space separated punctuations.
// Expected return is 0
@Test
@DisplayName("Count punctuation")
public void testCount6() {
manipulatedstring.setString("! ? ] ");
int length = manipulatedstring.count();
assertEquals(0, length);
}

// Test when string has numerical value.
// Expected return is 1
@Test
@DisplayName("Count numeric")
public void testCount7() {
manipulatedstring.setString("! 123 ");
int length = manipulatedstring.count();
assertEquals(1, length);
}

// Test if the method removes the right characters without maintaining spacing
@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 expected = "I' bttr uts0e 16tsinths trn6 rgh?";
String actual = manipulatedstring.removeNthCharacter(3, false);
assertEquals(expected, actual);
}

// Test if the method removes the right characters while maintaining spacing
@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 expected = "I' b tt r ut s0 e 16 ts in th s tr n6 r gh ?";
String actual = manipulatedstring.removeNthCharacter(3, true);
assertEquals(expected, actual);
}

// Throw IndexOutOfBoundException if the given index is exceeds the size of the string
@Test
public void testRemoveNthCharacter3() {
fail("Not yet implemented");
manipulatedstring.setString("This is 28 character string.");
assertThrows(IndexOutOfBoundsException.class, () -> {
manipulatedstring.removeNthCharacter(29, true);});
}

// Throw IndexOutOfBoundsException if the given index is less than or equal to 0
@Test
public void testRemoveNthCharacter4() {
fail("Not yet implemented");
manipulatedstring.setString("This is 28 character string.");
assertThrows(IllegalArgumentException.class, () -> {
manipulatedstring.removeNthCharacter(0, true);});
}

// Deleting all characters without maintaining spacing.
@Test
public void testRemoveNthCharacter5() {
fail("Not yet implemented");
manipulatedstring.setString("This is 28 character string.");
String expected = "";
String actual = manipulatedstring.removeNthCharacter(1, false);
assertEquals(expected, actual);
}

// Deleting each character while maintaining spacing.
@Test
public void testRemoveNthCharacter6() {
fail("Not yet implemented");
manipulatedstring.setString("This is 28 character string.");
String expected = " ";
String actual = manipulatedstring.removeNthCharacter(1, true);
assertEquals(expected, actual);
}

// Deleting only the last character without adding a space.
@Test
public void testRemoveNthCharacter7() {
fail("Not yet implemented");
manipulatedstring.setString("This is 28 character string.");
String expected = "This is 28 character string";
String actual = manipulatedstring.removeNthCharacter(28, false);
assertEquals(expected, actual);
}

@Test
Expand All @@ -87,63 +160,101 @@ public void testGeSubStrings1() {
assertEquals(sStings[1], "string");
}

// Test when the startWord index is less than or equal to 0
// Throws IllegalArgumentException
@Test
public void testGeSubStrings2() {
fail("Not yet implemented");
manipulatedstring.setString("This is my string");
assertThrows(IllegalArgumentException.class, () -> {
manipulatedstring.getSubStrings(0, 2);});
}

// Test when the endWord index is less than or equal to 0
// Throws IllegalArgumentException
@Test
public void testGeSubStrings3() {
fail("Not yet implemented");
manipulatedstring.setString("This is my string");
assertThrows(IllegalArgumentException.class, () -> {
manipulatedstring.getSubStrings(1, 0);});
}

// Test when the startWord index is greater than endWord index
// Throws IllegalArgumentException
@Test
public void testGeSubStrings4() {
fail("Not yet implemented");
manipulatedstring.setString("This is my string");
assertThrows(IllegalArgumentException.class, () -> {
manipulatedstring.getSubStrings(2, 1);});
}

// Test when the endWord index is greater than the word count of string
// Throws IndexOutOfBoundException
@Test
public void testGeSubStrings5() {
fail("Not yet implemented");
manipulatedstring.setString("This is my string");
assertThrows(IndexOutOfBoundsException.class, () -> {
manipulatedstring.getSubStrings(2, 5);});
}

// Test when the startWord and endWord are equal
@Test
public void testGeSubStrings6() {
fail("Not yet implemented");
manipulatedstring.setString("This is my string");
String [] sStings = manipulatedstring.getSubStrings(4, 4);
String expected = "string";
String actual = sStings[0];

assertEquals(expected, actual);
}

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

// Throw IllegalArgumentException if the indices length is greater than the string length. {5,6,7,8,9,10,11,0,1,2,3,4,12};
@Test
public void testRestoreString2()
{
fail("Not yet implemented");

manipulatedstring.setString("JUnitTesting!");
int[] indices = {5,6,7,8,9,10,11};
assertThrows(IllegalArgumentException.class, () -> {
manipulatedstring.restoreString(indices);});
}

// Throw IndexOutOfBoundsException if any indice is less than 0.
@Test
public void testRestoreString3()
{
fail("Not yet implemented");

manipulatedstring.setString("JUnitTesting!");
int[] indices = {5,6,7,8,9,10,11,0,1,2,3,4,-8};
assertThrows(IndexOutOfBoundsException.class, () -> {
manipulatedstring.restoreString(indices);});
}

// Throw IndexOutOfBoundsException if any indice is greater than the string length
@Test
public void testRestoreString4()
{
fail("Not yet implemented");

manipulatedstring.setString("JUnitTesting!");
int[] indices = {5,6,7,8,9,10,11,0,1,2,3,4,13};
assertThrows(IndexOutOfBoundsException.class, () -> {
manipulatedstring.restoreString(indices);});
}

// Test when each character is the same in the string

Choose a reason for hiding this comment

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

This is an interesting test case, I wonder in what ways it would break/why it would be needed

Copy link
Author

Choose a reason for hiding this comment

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

Yes, it is an interesting test case. I am not sure how you would break this case too. I run out of ideas and made up a case lol. Maybe it could be useful when implementation takes shortcuts or makes assumptions if the characters are the same. This test case ensures that the code does not make invalid assumptions.

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

manipulatedstring.setString("aaa");
int [] indices = {1,0,2};
String restoreString = manipulatedstring.restoreString(indices);
assertEquals(restoreString, "aaa");
}

}