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
138 changes: 115 additions & 23 deletions src/main/java/StringManipulation.java
Original file line number Diff line number Diff line change
@@ -1,33 +1,125 @@
public class StringManipulation implements StringManipulationInterface {
private String str;

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

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

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

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

Choose a reason for hiding this comment

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

I really like how you used the split method with regex to get the appropriate count of words. I will definitely be implementing this into my own code. Thank you

return splitStr.length;
/*
* int wordCount = 0;
boolean whitespace = false;

@Override
public String removeNthCharacter(int n, boolean maintainSpacing) {
return null;
}
// Iterate through each character in the string
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);

@Override
public String[] getSubStrings(int startWord, int endWord) {
return null;
}
// Check if the character is a whitespace
if (Character.isWhitespace(c)) {
whitespace = true;

// Check if the character is a letter or digit
if (Character.isLetterOrDigit(str.charAt(i+1))) {
// If we are not inside a word, increment the word count
if (whitespace) {
wordCount++;
whitespace = false;
}
}*/
}

@Override
public String restoreString(int[] indices) {
return null;
}
@Override
public String removeNthCharacter(int n, boolean maintainSpacing) {
if (n <= 0) {
throw new IllegalArgumentException("'n' must be greater than zero.");
}

if (n > str.length()) {
throw new IndexOutOfBoundsException("'n' is greater than the string length.");
}

String result = new String();
result = result.concat(str.charAt(0) + "");

Choose a reason for hiding this comment

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

This portion of the for loop in the removeNthCharacter() method seems to have some redundant code. Is there an advantage to starting your for loop at index = 1?

for (int i = 1; i < str.length(); i++) {
if((i+1) % n != 0) {
result = result.concat(str.charAt(i) + "");

Choose a reason for hiding this comment

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

This is personal preference but I like using += when concatenating strings together. Personally I think that nesting methods inside one another makes code harder to read.

}
else {
if (maintainSpacing) {
result = result.concat(" ");
}
}
}

return result;
}

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

// Split the sentence into words
String[] words = str.split("\\s+");

// Check if the sentence has enough words
if (endWord > words.length) {
throw new IndexOutOfBoundsException("The String has less than " + endWord + " words");
}

// Calculate the number of words to put in the array
int wordCount = endWord - startWord + 1;

// Create a new array to store the extracted words
String[] subStrArr = new String[wordCount];

// Extract the words from the specified positions
for (int i = 0; i < wordCount; i++) {
subStrArr[i] = words[startWord - 1 + i];
}

return subStrArr;
}

@Override
public String restoreString(int[] indices) {
//throws IllegalArgumentException if not s.length == indices.length == n
// throws IndexOutOfBoundsException if indices[i]< 0 or indices[i]> string length
if(str.length() != indices.length) {
throw new IllegalArgumentException("The indices array must be the same length as the string.");
}
char[] temp = str.toCharArray();
char[] shuffled = new char[str.length()];
String output = new String();
for(int i = 0; i < str.length(); i++) {
if(i < indices.length) {
int pos = indices[i];
if(pos < 0 || pos > str.length()) {
throw new IndexOutOfBoundsException("All values in the indices array must correspond to valid indexes in the string.");
}
shuffled[pos] = temp[i];
}
else{
shuffled[i] = temp[i];
}
}
output = String.valueOf(shuffled);
return output;
}

}
6 changes: 3 additions & 3 deletions src/main/java/StringManipulationInterface.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,10 @@ public interface StringManipulationInterface {
* The string s will be shuffled such that the character at the ith position moves to indices[i] in the shuffled string.
* Return the shuffled string.
* example:
* Input: string = "UnitTest", indices = [4,5,6,7,0,1,2,3]
* Input: string = "UnitTest", indices = [4,5,6,7,0,2,1,3]
* Output: "TestUnit"
* Explanation:
* indices: 4 5 6 7 0 1 2 3
* indices: 4 5 6 7 0 2 1 3
* String: U n i t T e s t
* Actions to Shuffle: Shift U to 4th position, n to 5th position, i to 6th position ......
* Output: T e s t U n i t
Expand All @@ -95,7 +95,7 @@ public interface StringManipulationInterface {
* indices length is the same as the string length.
*
* throws IllegalArgumentException if not s.length == indices.length == n
* throws IndexOutOfBoundsException if indices[i]< 0 or indices[i]>= string length
* throws IndexOutOfBoundsException if indices[i]< 0 or indices[i]> string length
*
* @param indices is an integer array for shuffled string new indices positions
* the character at the ith position moves to indices[i] in the shuffled string.
Expand Down
130 changes: 101 additions & 29 deletions src/test/java/StringManipulationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,58 +28,89 @@ public void testCount1() {

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

@Test
public void testCount3() {
fail("Not yet implemented");
manipulatedstring.setString("23 and me");
int length = manipulatedstring.count();
assertEquals(3, length);
}

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

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

@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 temp = manipulatedstring.removeNthCharacter(3, false);
assertEquals("I' bttr uts0e 16tsinths trn6 rgh?", temp);
}

@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 temp = manipulatedstring.removeNthCharacter(3, true);
assertEquals("I' b tt r ut s0 e 16 ts in th s tr n6 r gh ?", temp);
}

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

assertEquals("I'd 3tt3 puts0med161s inthis5tr16, rght?", temp);
}

@Test
public void testRemoveNthCharacter4() {
fail("Not yet implemented");
manipulatedstring.setString("I'd b3tt3r put s0me d161ts in this 5tr1n6, right?");
assertEquals("I'd 3tt3 put s0me d161 s in this 5tr1 6, r ght?", manipulatedstring.removeNthCharacter(5, true));
}

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

Choose a reason for hiding this comment

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

I believe you can use the assertThrows() method without having to set it equal to a Throwable object. The assertThrow() method checks to see whether the method you provided in the lambda function returns the same Throwable as the Throwable you claim it will throw. Your way of checking definitely works.

assertEquals("'n' is greater than the string length.", exception.getMessage());
}

@Test
public void testRemoveNthCharacter6() {
fail("Not yet implemented");
manipulatedstring.setString("I'd b3tt3r put s0me d161ts in this 5tr1n6, right?");
Throwable exception = assertThrows(IllegalArgumentException.class, () -> manipulatedstring.removeNthCharacter(0, false));
assertEquals("'n' must be greater than zero.", exception.getMessage());
}

@Test
public void testRemoveNthCharacter7() {
fail("Not yet implemented");
manipulatedstring.setString("I'd b3tt3r put s0me d161ts in this 5tr1n6, right?");
Throwable exception = assertThrows(IllegalArgumentException.class, () -> manipulatedstring.removeNthCharacter(-1, false));
assertEquals("'n' must be greater than zero.", exception.getMessage());
}

@Test
public void testGeSubStrings1() {
public void testRemoveNthCharacter8() {
manipulatedstring.setString("I'd b3tt3r put s0me d161ts in this 5tr1n6, right?");
assertEquals("I'd b3tt3r put s0me d161ts in this 5tr1n6, right", manipulatedstring.removeNthCharacter(49, false));
}

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

Expand All @@ -88,24 +119,49 @@ public void testGeSubStrings1() {
}

@Test
public void testGeSubStrings2() {
fail("Not yet implemented");
public void testGetSubStrings2() {
manipulatedstring.setString("Thisismystring");
Throwable exception = assertThrows(Exception.class, () -> manipulatedstring.getSubStrings(3, 4));
assertEquals("The String has less than 4 words", exception.getMessage());
}
@Test
public void testGeSubStrings3() {
fail("Not yet implemented");
public void testGetSubStrings3() {
manipulatedstring.setString("");
Throwable exception = assertThrows(Exception.class, () -> manipulatedstring.getSubStrings(1, 4));
assertEquals("The String has less than 4 words", exception.getMessage());
}
@Test
public void testGeSubStrings4() {
fail("Not yet implemented");
public void testGetSubStrings4() {
manipulatedstring.setString("This is my string");
String [] sStings = manipulatedstring.getSubStrings(3, 3);

assertEquals(sStings[0], "my");
}
@Test
public void testGeSubStrings5() {
fail("Not yet implemented");
public void testGetSubStrings5() {
manipulatedstring.setString("This is my string");
Throwable exception = assertThrows(IllegalArgumentException.class, () -> manipulatedstring.getSubStrings(5, 1));
assertEquals("Invalid startWord or endWord", exception.getMessage());
}
@Test
public void testGeSubStrings6() {
fail("Not yet implemented");
public void testGetSubStrings6() {
manipulatedstring.setString("This is my string");
Throwable exception = assertThrows(IndexOutOfBoundsException.class, () -> manipulatedstring.getSubStrings(1, 100));
assertEquals("The String has less than 100 words", exception.getMessage());
}

@Test
public void testGetSubStrings7() {
manipulatedstring.setString("This is my string");
Throwable exception = assertThrows(IllegalArgumentException.class, () -> manipulatedstring.getSubStrings(-1, 3));
assertEquals("Invalid startWord or endWord", exception.getMessage());
}

@Test
public void testGetSubStrings8() {
manipulatedstring.setString("This is my string");
Throwable exception = assertThrows(IllegalArgumentException.class, () -> manipulatedstring.getSubStrings(1, -3));
assertEquals("Invalid startWord or endWord", exception.getMessage());
}

@Test
Expand All @@ -121,28 +177,44 @@ public void testRestoreString1()
@Test
public void testRestoreString2()
{
fail("Not yet implemented");

manipulatedstring.setString("This is my string");

int [] array;
array=new int[]{1,0,2,5,3,4,7,6};
Throwable exception = assertThrows(IllegalArgumentException.class, () -> manipulatedstring.restoreString(array) );
assertEquals("The indices array must be the same length as the string.", exception.getMessage());
}

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

manipulatedstring.setString("This is my string");
int [] array;
array=new int[]{-1,0,2,5,3,4,7,6,13,15,16,9,10,12,8,14,11};
Throwable exception = assertThrows(IndexOutOfBoundsException.class, () -> manipulatedstring.restoreString(array) );
assertEquals("All values in the indices array must correspond to valid indexes in the string.", exception.getMessage());
}

@Test
public void testRestoreString4()
{
fail("Not yet implemented");
manipulatedstring.setString("This is my string");
int [] array;
array=new int[]{1,0,2,5,3,4,7,6,13,15,16,9,10,12,8,100,11};
Throwable exception = assertThrows(IndexOutOfBoundsException.class, () -> manipulatedstring.restoreString(array) );
assertEquals("All values in the indices array must correspond to valid indexes in the string.", exception.getMessage());


}

@Test
public void testRestoreString5()
{
fail("Not yet implemented");
manipulatedstring.setString("second test ");
int [] array;
array=new int[]{11,10,9,8,7,6,5,4,3,2,1,0};
String restoreString = manipulatedstring.restoreString(array);
assertEquals(restoreString, " tset dnoces");

}

Expand Down