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
120 changes: 111 additions & 9 deletions src/main/java/StringManipulation.java
Original file line number Diff line number Diff line change
@@ -1,33 +1,135 @@
public class StringManipulation implements StringManipulationInterface {


public class StringManipulation implements StringManipulationInterface {

private String myString;

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

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

@Override
public int count() {
return 0;
if (myString == null) {
throw new NullPointerException("Cannot count uninitialized string.");
}
if (myString.isEmpty() || myString.isBlank()) {
return 0;
}
String[] arrOfStr = myString.split(" ");
int countOfWords = arrOfStr.length;
return countOfWords;
}

@Override
public String removeNthCharacter(int n, boolean maintainSpacing) {
return null;
char[] chars = myString.toCharArray();
String str = null;

if (n > chars.length) {
throw new IndexOutOfBoundsException("n cannot be greater than the length of the string");
}

if (n <= 0) {
throw new IllegalArgumentException("n must be greater than 0");
}

if(maintainSpacing) {
if (n==1) {
for (int i = 0; i<chars.length; i++) {
chars[i] = ' ';
}
}
else if (n>=1) {
for (int i = 0; i<=chars.length; i=i+n) {
if (i == 0) {
continue;
}
int index = i-1;
chars[index] = ' ';
}
}
str = String.copyValueOf(chars);
}
else if (!maintainSpacing)
{
if (n==1) {
return null;
}
char[] temp = new char[chars.length-((int)Math.floor(chars.length/n))];
if (n>=1)
{
for (int i = 0, k=0; i<chars.length && k<temp.length; i++, k++) {
for (int j = 0; j<chars.length; j=j+n) {
int index = j-1;
if (i == 0) {
break;
}
if (index==i) {
i++;
break;
}
}
temp[k]=chars[i];
}
}
str = String.copyValueOf(temp);
}
return str;
}

@Override
public String[] getSubStrings(int startWord, int endWord) {
return null;
public String[] getSubStrings(int startWord, int endWord){
if (startWord > endWord || startWord <= 0 || endWord <= 0) {
throw new IllegalArgumentException("startWord must be greater than endWord. Both startWord and endWord must be greater than 0.");
}

String[] substr = myString.split(" ");

if (substr.length < endWord) {
throw new IndexOutOfBoundsException("endWord cannot be greater than length of substr");
}

String[] temp = new String[endWord-startWord+1];
for (int i = startWord-1, j = 0; i<endWord && j<temp.length; i++, j++) {
temp[j] = substr[i];
}
return temp;
}

@Override
public String restoreString(int[] indices) {
return null;
}
public String restoreString(int[] indices){
if (myString.length() != indices.length) {
throw new IllegalArgumentException("the number of indices provides must match positions in the string");
}

int sumOfIndexes = 0;
int k = 0;
for (int i = 0; i<indices.length; i++)
{
if (indices[i] > myString.length() || indices[i] < 0) {
throw new IndexOutOfBoundsException("indices must be within length of the string");
}
sumOfIndexes = sumOfIndexes + indices[i];
k=k+i;
}
if (sumOfIndexes != k) {
throw new IllegalArgumentException("incorrect indexes provided");
}

char[] chars = myString.toCharArray();
char[] temp = new char[chars.length];
for (int i = 0; i<temp.length; i++) {
temp[i] = chars[indices[i]];
}

String str = String.copyValueOf(temp);
return str;
}
}
8 changes: 5 additions & 3 deletions src/main/java/StringManipulationInterface.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@


/**
* This is an interface for a simple class that represents a string, defined
* as a sequence of characters.
Expand Down Expand Up @@ -79,10 +81,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 +97,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
Loading