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
79 changes: 73 additions & 6 deletions src/main/java/StringManipulation.java
Original file line number Diff line number Diff line change
@@ -1,32 +1,99 @@
import java.util.Arrays;

public class StringManipulation implements StringManipulationInterface {

private String string;

@Override
public String getString() {
return null;
return this.string;
}

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

@Override
public int count() {
return 0;
//If string is null or blank, return 0.
if(this.string == null || this.string.isBlank()) return 0;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This is great i should have done this in my code it fairly simple and works


/*
Otherwise, get the number of words in string, which are
separated by one or more whitespace characters.
*/
return string.split("\\s+").length;
}

@Override
public String removeNthCharacter(int n, boolean maintainSpacing) {
return null;
//check null string
if(this.string == null) return null;

// check n <= 0
if(n <= 0) throw new IllegalArgumentException("N must be greater than 0");

// check n > string string
if(n > this.string.length()) throw new IndexOutOfBoundsException("Index out of bounds");

//edge cases
if(n == 1 && !maintainSpacing) return "";
if(n == 1 && maintainSpacing) return this.string.replaceAll(".", " ");
Comment on lines +41 to +42

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

you could make this as an inbulit feature of the for loop to save space and handle edge case


// builds the string. every multiple of n is either
// replaced with whitespace or removed from the result,
// depending on the boolean value passed.
StringBuilder b = new StringBuilder();
for(int i = 0; i < this.string.length(); i++) {
if((i+1) % n == 0) {
if(maintainSpacing) b.append(' ');
} else {
b.append(this.string.charAt(i));
}
}
return b.toString();
}

@Override
public String[] getSubStrings(int startWord, int endWord) {
return null;
public String[] getSubStrings(int startWord, int endWord){
//check null string
if(this.string == null) return null;

//check startWord <= 0
if(startWord <= 0) throw new IllegalArgumentException("startWord must be greater than zero");

//check endWord <= 0
if(endWord <= 0) throw new IllegalArgumentException("endWord must be greater than zero");

//check startWord > endWord
if(startWord > endWord) throw new IllegalArgumentException("startWord cannot be larger than endWord");

//check string.count() < endWord
if(this.count() < endWord) throw new IndexOutOfBoundsException("endWord cannot be larger than the total word count");

return Arrays.copyOfRange(this.string.split("\\s+"), startWord-1, endWord);
}

@Override
public String restoreString(int[] indices) {
return null;
//check null string
if(this.string == null) return null;

//check unequal lengths
if(this.string.length() != indices.length) throw new IllegalArgumentException("String length must match array length");

//check valid indices
for(int i = 0; i < indices.length; i++) {
if(indices[i] < 0 || indices[i] >= this.string.length()) throw new IndexOutOfBoundsException("Invalid array index");
}

//average case
StringBuilder b = new StringBuilder();
for(int i = 0; i < indices.length; i++) {
b.append(this.string.charAt(indices[i]));
}
return b.toString();
}


Expand Down
182 changes: 144 additions & 38 deletions src/test/java/StringManipulationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,95 +19,193 @@ public void tearDown() {
manipulatedstring = null;
}

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

//Empty string case
@Test
public void testCount2() {
fail("Not yet implemented");
manipulatedstring.setString("");
int length = manipulatedstring.count();
assertEquals(0, length);
}

//Null string case
@Test
public void testCount3() {
fail("Not yet implemented");
manipulatedstring.setString(null); //Not really needed (because string starts null), but kept for clarity.
int length = manipulatedstring.count();
assertEquals(0, length);
}

//One word case
@Test
public void testCount4() {
fail("Not yet implemented");
manipulatedstring.setString("Supercalifragilisticexpialidocious");
int length = manipulatedstring.count();
assertEquals(1, length);
}

//Only spaces case
@Test
public void testCount5() {
manipulatedstring.setString(" ");
int length = manipulatedstring.count();
assertEquals(0, length);
}

//Only tab case
@Test
public void testCount6() {
manipulatedstring.setString("\t\t\t");
int length = manipulatedstring.count();
assertEquals(0, length);
}

//Only carriage return case
@Test
public void testCount7() {
manipulatedstring.setString("\r");
int length = manipulatedstring.count();
assertEquals(0, length);
}

//Only newlines case
@Test
public void testCount8() {
manipulatedstring.setString("\n\n\n");
int length = manipulatedstring.count();
assertEquals(0, length);
}

//Multiple spaces between words case
@Test
public void testCount9() {
manipulatedstring.setString("Lots of space between");
int length = manipulatedstring.count();
assertEquals(4, length);
}

//Do not maintain spacing case
@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));
}

//Maintain spacing case
@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));
}

//Throws IndexOutOfBoundsException when n larger than string length

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

testing looks good but i would add for exception test for when it true just to double check if it works

@Test
public void testRemoveNthCharacter3() {
fail("Not yet implemented");
manipulatedstring.setString("abcdef");
Exception e = assertThrows(IndexOutOfBoundsException.class, () -> manipulatedstring.removeNthCharacter(7, false));
assertEquals("Index out of bounds", e.getMessage());
}

//Throws IllegalArgumentException when n is negative
@Test
public void testRemoveNthCharacter4() {
fail("Not yet implemented");
manipulatedstring.setString("foobar");
Exception e = assertThrows(IllegalArgumentException.class, () -> manipulatedstring.removeNthCharacter(-2, false));
assertEquals("N must be greater than 0", e.getMessage());
}

//Throws IllegalArgumentException when n is zero
@Test
public void testRemoveNthCharacter5() {
fail("Not yet implemented");
manipulatedstring.setString("foobar");
Exception e = assertThrows(IllegalArgumentException.class, () -> manipulatedstring.removeNthCharacter(0, false));
assertEquals("N must be greater than 0", e.getMessage());
}

//n=1, do not maintain spacing case
@Test
public void testRemoveNthCharacter6() {
fail("Not yet implemented");
manipulatedstring.setString("12345 6789");
String result = manipulatedstring.removeNthCharacter(1, false);
int length = result.length();
assertEquals(0, length);
}

//n=1, maintain spacing case
@Test
public void testRemoveNthCharacter7() {
fail("Not yet implemented");
manipulatedstring.setString("12345 6789");
String result = manipulatedstring.removeNthCharacter(1, true);
int length = result.length();
assertEquals(10, length);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

did not think about doing this test in my own code looks good

}

//null string case
@Test
public void testRemoveNthCharacter8() {
manipulatedstring.setString(null);
String s = manipulatedstring.removeNthCharacter(3, true);
assertNull(s);
}

//Average case
@Test
public void testGeSubStrings1() {
public void testGetSubStrings1() {
manipulatedstring.setString("This is my string");
String [] sStings = manipulatedstring.getSubStrings(3, 4);
String [] sStrings = manipulatedstring.getSubStrings(3, 4);

assertEquals(sStings[0], "my");
assertEquals(sStings[1], "string");
assertEquals(sStrings[0], "my");
assertEquals(sStrings[1], "string");
}

//Throws IllegalArgumentException when startword <= 0
@Test
public void testGeSubStrings2() {
fail("Not yet implemented");
public void testGetSubStrings2() {
manipulatedstring.setString("It's beginning to look a lot like Christmas");
Exception e = assertThrows(IllegalArgumentException.class, () -> manipulatedstring.getSubStrings(-1, 3));
assertEquals("startWord must be greater than zero", e.getMessage());
}

//Throws IllegalArgumentException when endword <= 0
@Test
public void testGeSubStrings3() {
fail("Not yet implemented");
public void testGetSubStrings3() {
manipulatedstring.setString("I used to wish I was, but I'm glad I'm not");
Exception e = assertThrows(IllegalArgumentException.class, () -> manipulatedstring.getSubStrings(1, -3));
assertEquals("endWord must be greater than zero", e.getMessage());
}

//Throws IllegalArgumentException when startword > endword
@Test
public void testGeSubStrings4() {
fail("Not yet implemented");
public void testGetSubStrings4() {
manipulatedstring.setString("I know the roads to riches and I know the ways to fame");
Exception e = assertThrows(IllegalArgumentException.class, () -> manipulatedstring.getSubStrings(7, 4));
assertEquals("startWord cannot be larger than endWord", e.getMessage());
}

//Throws IndexOutOfBoundsException when string.count() < endword
@Test
public void testGeSubStrings5() {
fail("Not yet implemented");
public void testGetSubStrings5() {
manipulatedstring.setString("Balloons are deflating");
Exception e = assertThrows(IndexOutOfBoundsException.class, () -> manipulatedstring.getSubStrings(1, 4));
assertEquals("endWord cannot be larger than the total word count", e.getMessage());
}

//null string
@Test
public void testGeSubStrings6() {
fail("Not yet implemented");
public void testGetSubStrings6() {
manipulatedstring.setString(null);
String[] array = manipulatedstring.getSubStrings(2, 4);
assertNull(array);
}

//Average case
@Test
public void testRestoreString1()
{
Expand All @@ -118,32 +216,40 @@ public void testRestoreString1()
assertEquals(restoreString, "rat");
}

//unequal lengths case. Throws IllegalArgumentException
@Test
public void testRestoreString2()
{
fail("Not yet implemented");

public void testRestoreString2() {
manipulatedstring.setString("extraordinary");
int[] indices = {0,1,2,3,4};
Exception e = assertThrows(IllegalArgumentException.class, () -> manipulatedstring.restoreString(indices));
assertEquals("String length must match array length", e.getMessage());
}

//negative index. Throws IndexOutOfBoundsException
@Test
public void testRestoreString3()
{
fail("Not yet implemented");

public void testRestoreString3() {
manipulatedstring.setString("tubular");
int[] indices = {-1,2,3,4,5,6,7};
Exception e = assertThrows(IndexOutOfBoundsException.class, () -> manipulatedstring.restoreString(indices));
assertEquals("Invalid array index", e.getMessage());
}

//index > string length. Throws IndexOutOfBoundsException
@Test
public void testRestoreString4()
{
fail("Not yet implemented");

public void testRestoreString4() {
manipulatedstring.setString("radical");
int[] indices = {1,2,3,4,5,6,10};
Exception e = assertThrows(IndexOutOfBoundsException.class, () -> manipulatedstring.restoreString(indices));
assertEquals("Invalid array index", e.getMessage());
}

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

public void testRestoreString5() {
manipulatedstring.setString(null);
int[] indices = {1,2,3,4};
String result = manipulatedstring.restoreString(indices);
assertNull(result);
}

}