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
113 changes: 107 additions & 6 deletions src/main/java/StringManipulation.java
Original file line number Diff line number Diff line change
@@ -1,33 +1,134 @@
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)
return 0;
if (string.length() == 0)
return 0;

int count = 0;
for (int i = 0; i < string.length() - 2; i++) {
if (i == 0 && !isSpaceChar(string.charAt(i)))
count++;
else if (isSpaceChar(string.charAt(i)) && !isSpaceChar(string.charAt(i + 1)))
count++;
}

return count;
Comment on lines +17 to +30

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pretty laborious solution, I like the meticulousness and I see the hussle, but like I say in a later comment you can use the split method of the string class with the regex "\s+", giving you a string array of everything split by whitespaces, and with that you could just return the length of that generated array, easy peasy.

}

@Override
public String removeNthCharacter(int n, boolean maintainSpacing) {
return null;
if (n <= 0)
throw new IllegalArgumentException();
if (n > string.length())
throw new IndexOutOfBoundsException();

StringBuilder newStr = new StringBuilder();
for (int i = 1; i < string.length() + 1; i++) {
if (i % n == 0) {
if (maintainSpacing)
newStr.append(' ');
} else
newStr.append(string.charAt(i - 1));
}

return newStr.toString();
}

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

String[] words = new String[(endWord - startWord) + 1];
int wordIndex = 0;

StringBuilder sb = new StringBuilder();
int wordCount = 0;
for (int i = 0; i < string.length() - 1; i++) {
// Case where first character is a word
if (i == 0 && !isSpaceChar(string.charAt(i))) {
wordCount++;
if (wordCount == startWord)
sb.append(string.charAt(i));

// Case where beginning of new word is found
} else if (isSpaceChar(string.charAt(i)) && !isSpaceChar(string.charAt(i + 1))) {
wordCount++;
i++;
if (wordCount >= startWord && wordCount <= endWord)
sb.append(string.charAt(i));

// Case where end of word is found
} else if (!isSpaceChar(string.charAt(i)) && isSpaceChar(string.charAt(i + 1))) {
if (wordCount >= startWord && wordCount <= endWord) {
sb.append(string.charAt(i));
words[wordIndex++] = sb.toString();
sb = new StringBuilder();
}

} else if (!isSpaceChar(string.charAt(i)) && wordCount >= startWord && wordCount <= endWord) {
sb.append(string.charAt(i));
Comment on lines +59 to +87

@WNeil WNeil Jun 14, 2023

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

StringBuilder is a good choice, I however favored the split method of the string class using the regex \s+, this gives you an array of all the char groups between whitespace gaps, then you can just loop through that array and add desired words to your final words array. that solution is a bit shorter to write and simpler IMHO

}
}
if (endWord == count()) {
sb.append(string.charAt(string.length() - 1));
words[wordIndex] = sb.toString();
}

return words;
}

@Override
public String restoreString(int[] indices) {
return null;
public String restoreString(int[] indices){
if (indices.length != string.length())
throw new IllegalArgumentException();

boolean[] indexUsed = new boolean[indices.length];
char[] chars = new char[indices.length];
for (int i = 0; i < indices.length; i++) {
if (indices[i] < 0 || indices[i] > string.length())
throw new IndexOutOfBoundsException();
if (indexUsed[indices[i]])
throw new IllegalArgumentException("Indices must be unique");

chars[indices[i]] = string.charAt(i);
indexUsed[indices[i]] = true;
}

StringBuilder sb = new StringBuilder();
for (char c : chars)
sb.append(c);

return sb.toString();
Comment on lines +99 to +119

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Wow wow wow, I really like the boolean array decision, very smart idea to catch something I did not even account for! I think you can construct a new String from a char array without needing to pass it through a StringBuilder, making a new String object with the char array as a param, otherwise really good job!

}


private boolean isSpaceChar(char ch) {
// Add any characters considered as a word delimiter
char[] spaceChars = {' ', '_', '-', '\t', '\n'};

for (char c : spaceChars) {
if (c == ch)
return true;
}

return false;
}
}
Loading