-
Notifications
You must be signed in to change notification settings - Fork 6
update #35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
update #35
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
This file was deleted.
| 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) { | ||
| 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); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
| } | ||
| } | ||
| 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); | ||
| } |
There was a problem hiding this comment.
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.