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
33 changes: 0 additions & 33 deletions src/main/java/StringManipulation.java

This file was deleted.

108 changes: 0 additions & 108 deletions src/main/java/StringManipulationInterface.java

This file was deleted.

66 changes: 66 additions & 0 deletions src/src/main/java/StringManipulation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import java.util.*;

public class StringManipulation implements StringManipulationInterface {
private String s;

@Override
public void setString(String s) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The code checks for null or empty string in the count() and getSubStrings() methods, which is good. However, it doesn't handle null or empty string input in the setString() method. You might want to add a check for that.

this.s = s;
}

@Override
public int count() {
if (s == null || s.length() == 0) {
return 0;
}
return s.trim().split("\\s+").length;
}

@Override
public String removeNthCharacter(int n, boolean includeSpaces) {
StringBuilder result = new StringBuilder();

for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);

if ((includeSpaces && c != ' ') || !includeSpaces) {
if ((i + 1) % n != 0) {
result.append(c);
} else if (includeSpaces) {
result.append(' ');
}
} else {
result.append(c);
}
}

return result.toString();
}


@Override
public String[] getSubStrings(int start, int end) {
if (s == null || s.length() == 0) {
return new String[0];
}
String[] words = s.split("\\s+");
if (start < 0 || end > words.length || start > end) {
return new String[0];
}
return Arrays.copyOfRange(words, start, end+1);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The getSubStrings() method splits the string into words using \s+ as the delimiter. It then returns a substring array based on the provided start and end indices. However, the end index is exclusive in Arrays.copyOfRange(), so you could use end instead of end+1 when copying the range

}

@Override
public String restoreString(int[] array) {
if (s == null || array == null) {
return s;
}
char[] arr = new char[s.length()];
for (int i = 0; i < array.length; i++) {
if (array[i] < arr.length) {
arr[array[i]] = s.charAt(i);
}
}
return new String(arr);
}
}
16 changes: 16 additions & 0 deletions src/src/main/java/StringManipulationInterface.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
public interface StringManipulationInterface {
// Method to set the string
void setString(String s);

// Method to count the number of words in the string
int count();

// Method to remove the nth character from the string, considering spaces as well or not
String removeNthCharacter(int n, boolean considerSpaces);

// Method to get an array of substrings of the string, from start to end (both inclusive)
String[] getSubStrings(int start, int end);

// Method to restore the string by the provided array
String restoreString(int[] array);
}
Loading