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
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
language: java
jdk: openjdk17
jdk: openjdk15
jobs:
include:
- script: mvn clean verify package
Expand Down
84 changes: 76 additions & 8 deletions src/main/java/StringManipulation.java
Original file line number Diff line number Diff line change
@@ -1,33 +1,101 @@
import java.util.*;
public class StringManipulation implements StringManipulationInterface {
private String pString;

@Override
public String getString() {
return null;
return pString;
}

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

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

String[] words = pString.split("\\s+"); //split string to words using space
return words.length;
}

@Override
public String removeNthCharacter(int n, boolean maintainSpacing) {
return null;
String theString = getString();

if(n > theString.length()){
throw new IndexOutOfBoundsException("N cannot be larger than the length of Sting");
}

if(n <= 0){
throw new IllegalArgumentException("Input n cannot be 0 or negative");
}

StringBuilder sb = new StringBuilder(theString);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Good idea using a StringBuilder! I hadn't thought of that in my implementation.

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.

But I forgot to add a exception when n is larger than the length of string lol


if(maintainSpacing){ //replace the nth letter with a space
for(int i = n-1; i < theString.length(); i += n){
sb.setCharAt(i, ' ');
}
return sb.toString();
}
else{
if(n == 1){ //return empty string
return "";
}
else{ //delete nth letter without spacing
for(int i = n-1; i < theString.length(); i += n){
while (i < sb.length()){
sb.deleteCharAt(i);
i += n - 1;
}
}
return sb.toString();
}
}
}

@Override
public String[] getSubStrings(int startWord, int endWord) {
return null;
public String[] getSubStrings(int startWord, int endWord){
if(startWord <= 0 || endWord <= 0 || startWord > endWord){
throw new IllegalArgumentException("Invalid startWord or endWord");
}

String[] words = getString().split("\\s+"); //split string to words using space

if(words.length < endWord){
throw new IndexOutOfBoundsException("String has less than 'endWord' words");
}
//returns a copy of array that only include startWord to endWord
return Arrays.copyOfRange(words, startWord - 1, endWord);
}

@Override
public String restoreString(int[] indices) {
return null;
}
public String restoreString(int[] indices){
if(indices == null || this.pString == null) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

May also want to check for string and indices[] with length zero as a precaution

throw new IllegalArgumentException("There are no input");
}

if(indices.length == 0 || this.pString.length() == 0){
throw new IllegalArgumentException("The indices or String is 0");
}

if(indices.length != this.pString.length()) {
throw new IllegalArgumentException("String length and indices length are not equal");
}

char[] shuffledString = new char[indices.length]; //creating new char array of length indices

for(int i = 0; i < indices.length; i++) { //for each index in indices
if(indices[i] < 0 || indices[i] >= this.pString.length()) {
throw new IndexOutOfBoundsException("The indices " + indices[i] + " is out of bounds");
}
shuffledString[i] = this.pString.charAt(indices[i]); //shuffles string in the order of indices
}

return new String(shuffledString);
}
}
127 changes: 90 additions & 37 deletions src/test/java/StringManipulationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,23 @@ public void testCount1() {

@Test
public void testCount2() {
fail("Not yet implemented");
manipulatedstring.setString("Testing for string");
int length = manipulatedstring.count();
assertEquals(3, length);
}

@Test
public void testCount3() {
fail("Not yet implemented");
manipulatedstring.setString("Testing testing");
int length = manipulatedstring.count();
assertEquals(2, length);
}

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

@Test
Expand All @@ -49,101 +55,148 @@ public void testRemoveNthCharacter1() {

@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));
manipulatedstring.setString("Hi this is the message");
assertEquals("H h s i h e s g ", manipulatedstring.removeNthCharacter(2, true));
}

@Test
public void testRemoveNthCharacter3() {
fail("Not yet implemented");
manipulatedstring.setString("This message should have less character deleted");
assertEquals("This me sage sh uld hav less c aracter deleted", manipulatedstring.removeNthCharacter(8, true));
}

@Test
public void testRemoveNthCharacter4() {
fail("Not yet implemented");
manipulatedstring.setString("This message should have only the last character deleted");
assertEquals("This message should have only the last character delete", manipulatedstring.removeNthCharacter(56, false));
}

@Test
public void testRemoveNthCharacter5() {
fail("Not yet implemented");
manipulatedstring.setString("This message should be unreadable");

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I'd recommend adding a test that checks to see what happens when N is a value larger than the length of the string

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.

yes I did that now

assertThrows(
IndexOutOfBoundsException.class, () -> {
assertEquals("", manipulatedstring.removeNthCharacter(100, false));
}
);
}

@Test
public void testRemoveNthCharacter6() {
fail("Not yet implemented");
manipulatedstring.setString("This message is meant to be deleted");
assertEquals("", manipulatedstring.removeNthCharacter(1, false));
}

@Test
public void testRemoveNthCharacter7() {
fail("Not yet implemented");
manipulatedstring.setString("The space");
assertEquals(" ", manipulatedstring.removeNthCharacter(1, true));
}

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

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

@Test
public void testGeSubStrings2() {
fail("Not yet implemented");
manipulatedstring.setString("This change is requested by Caleb");


assertThrows(
IllegalArgumentException.class, () -> {
String [] sStings = manipulatedstring.getSubStrings(6, 1);
}
);
}

@Test
public void testGeSubStrings3() {
fail("Not yet implemented");
manipulatedstring.setString("This is a odd string");
String [] sStings = manipulatedstring.getSubStrings(1, 3);

assertEquals("This", sStings[0]);
assertEquals("a", sStings[2]);
}

@Test
public void testGeSubStrings4() {
fail("Not yet implemented");
manipulatedstring.setString("This string is a very long string");
String [] sStings = manipulatedstring.getSubStrings(2, 4);

assertEquals("string", sStings[0]);
assertEquals("a", sStings[2]);
}

@Test
public void testGeSubStrings5() {
fail("Not yet implemented");
manipulatedstring.setString("This sting will return the first and last string");
String [] sStings = manipulatedstring.getSubStrings(1, 9);

assertEquals("This", sStings[0]);
assertEquals("string", sStings[8]);
}

@Test
public void testGeSubStrings6() {
fail("Not yet implemented");
manipulatedstring.setString("This string is the last test string");
Comment thread
carector marked this conversation as resolved.
assertThrows(
IndexOutOfBoundsException.class, () -> {
String [] sStings = manipulatedstring.getSubStrings(1, 100);
}
);
}

@Test
public void testRestoreString1()
{
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");
assertEquals("rat", restoreString);
}

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

public void testRestoreString2() {
manipulatedstring.setString("UnitTest");
int [] array;
array = new int[]{4,5,6,7,0,1,2,3};
String restoreString = manipulatedstring.restoreString(array);
assertEquals("TestUnit", restoreString);
}

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

public void testRestoreString3() {
manipulatedstring.setString("eat");
int [] array;
array = new int[]{2,0,1};
String restoreString = manipulatedstring.restoreString(array);
assertEquals("tea", restoreString);
}

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

public void testRestoreString4() {
manipulatedstring.setString("cat");
int [] array;
array=new int[]{1,0,2};
String restoreString = manipulatedstring.restoreString(array);
assertEquals("act", restoreString);
}

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

public void testRestoreString5() {
manipulatedstring.setString("");
int [] array;
array=new int[]{2,1,0};
assertThrows(
IllegalArgumentException.class, () -> {
String restoreString = manipulatedstring.restoreString(array);
}
);
}

}