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
70 changes: 63 additions & 7 deletions src/main/java/StringManipulation.java
Original file line number Diff line number Diff line change
@@ -1,33 +1,89 @@
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
public int count() {
return 0;
if (string == null || string.isEmpty()) {
return 0;
}

// Split the string by whitespace and count the number of words
String[] words = string.split("\\s+");
return words.length;
}

@Override
public String removeNthCharacter(int n, boolean maintainSpacing) {
return null;
if (string == null || string.isEmpty()) {
return string;
}

StringBuilder result = new StringBuilder();
char[] characters = string.toCharArray();

for (int i = 0; i < characters.length; i++) {
if ((i + 1) % n != 0) {
result.append(characters[i]);
} else if (maintainSpacing) {
result.append(characters[i]).append(" ");
}
}

return result.toString().trim();
}

@Override
public String[] getSubStrings(int startWord, int endWord) {
return null;
if (string == null || string.isEmpty()) {
return new String[0];
}

String[] words = string.split("\\s+");
if (startWord < 1 || startWord > words.length || endWord < 1 || endWord > words.length || startWord > endWord) {
throw new IllegalArgumentException("Invalid startWord or endWord");
}

int numSubStrings = endWord - startWord + 1;
String[] subStrings = new String[numSubStrings];

for (int i = 0; i < numSubStrings; i++) {
subStrings[i] = words[startWord - 1 + i];
}

return subStrings;
}

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

if (indices.length != string.length()) {
throw new IllegalArgumentException("Invalid indices length");
}

}
char[] characters = string.toCharArray();
char[] restored = new char[indices.length];

for (int i = 0; i < indices.length; i++) {
int index = indices[i];
if (index < 0 || index >= indices.length) {
throw new IllegalArgumentException("Invalid index value");
}
restored[index] = characters[i];
}

return new String(restored);
}
}
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
135 changes: 64 additions & 71 deletions src/test/java/StringManipulationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;


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

public class StringManipulationTest {
Expand All @@ -28,17 +27,9 @@ public void testCount1() {

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

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

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

@Test
Expand All @@ -49,101 +40,103 @@ 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("");
assertEquals("", manipulatedstring.removeNthCharacter(3, false));
}

@Test
public void testRemoveNthCharacter3() {
fail("Not yet implemented");
}
public void testGetSubStrings1() {
manipulatedstring.setString("This is my string");
String[] subStrings = manipulatedstring.getSubStrings(3, 4);

@Test
public void testRemoveNthCharacter4() {
fail("Not yet implemented");
assertEquals("my", subStrings[0]);
assertEquals("string", subStrings[1]);
}

@Test
public void testRemoveNthCharacter5() {
fail("Not yet implemented");
}
public void testGetSubStrings2() {
manipulatedstring.setString("This is my string");
String[] subStrings = manipulatedstring.getSubStrings(1, 1);

@Test
public void testRemoveNthCharacter6() {
fail("Not yet implemented");
assertEquals("This", subStrings[0]);
}

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

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

assertEquals(sStings[0], "my");
assertEquals(sStings[1], "string");
public void testRestoreString2() {
manipulatedstring.setString("");
int[] indices = new int[]{};
String restoreString = manipulatedstring.restoreString(indices);
assertEquals("", restoreString);
}

@Test
public void testGeSubStrings2() {
fail("Not yet implemented");
}
@Test
public void testGeSubStrings3() {
fail("Not yet implemented");
}
@Test
public void testGeSubStrings4() {
fail("Not yet implemented");
public void testCount3() {
manipulatedstring.setString("12345");
int length = manipulatedstring.count();
assertEquals(1, length);
}

@Test
public void testGeSubStrings5() {
fail("Not yet implemented");
public void testCount4() {
manipulatedstring.setString("One two three four");
int length = manipulatedstring.count();
assertEquals(4, length);
}

@Test
public void testGeSubStrings6() {
fail("Not yet implemented");
public void testRemoveNthCharacter3() {
manipulatedstring.setString("Hello, World!");
assertEquals("Helo, Wrld!", manipulatedstring.removeNthCharacter(2, false));
}

@Test
public void testRestoreString1()
{
manipulatedstring.setString("art");
int [] array;
array=new int[]{1,0,2};
String restoreString = manipulatedstring.restoreString(array);
assertEquals(restoreString, "rat");
public void testRemoveNthCharacter4() {
manipulatedstring.setString("Hello, World!");
assertEquals("Helo Wrld", manipulatedstring.removeNthCharacter(2, true));
}

@Test
public void testRestoreString2()
{
fail("Not yet implemented");
public void testGetSubStrings3() {
manipulatedstring.setString("This is my string");
String[] subStrings = manipulatedstring.getSubStrings(2, 3);

assertEquals("is", subStrings[0]);
assertEquals("my", subStrings[1]);
}

@Test
public void testRestoreString3()
{
fail("Not yet implemented");
public void testGetSubStrings4() {
manipulatedstring.setString("One two three four five");
String[] subStrings = manipulatedstring.getSubStrings(1, 5);

assertEquals("One", subStrings[0]);
assertEquals("two", subStrings[1]);
assertEquals("three", subStrings[2]);
assertEquals("four", subStrings[3]);
assertEquals("five", subStrings[4]);
}

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

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

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

public void testRestoreString4() {
manipulatedstring.setString("hello");
int[] indices = new int[]{4, 3, 2, 1, 0};
String restoreString = manipulatedstring.restoreString(indices);
assertEquals("olleh", restoreString);
}

}
}