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
11 changes: 11 additions & 0 deletions .project
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,15 @@
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
<filteredResources>
<filter>
<id>1730318746054</id>
<name></name>
<type>30</type>
<matcher>
<id>org.eclipse.core.resources.regexFilterMatcher</id>
<arguments>node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>
</matcher>
</filter>
</filteredResources>
</projectDescription>
12 changes: 12 additions & 0 deletions src/com/jwetherell/algorithms/strings/StringFunctions.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ public class StringFunctions {
private static final char SPACE = ' ';

public static final String reverseWithStringConcat(String string) {

// Check if the input string is null or empty, and return an empty string if true
if (string == null || string.isEmpty()) {
Copy link
Author

Choose a reason for hiding this comment

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

I added this if block so that if the string is null or if the string is empty the program can handle these cases without crashing or outputting an unexpected output.

return "";
}

String output = new String();
for (int i = (string.length() - 1); i >= 0; i--) {
output += (string.charAt(i));
Expand All @@ -21,6 +27,12 @@ public static final String reverseWithStringConcat(String string) {
}

public static final String reverseWithStringBuilder(String string) {

// Check if the input string is null or empty, and return an empty string if true
if (string == null || string.isEmpty()) {
Copy link
Author

Choose a reason for hiding this comment

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

I added the the same if block here because this method also needs the same check as the previous regarding the null and empty string cases.

return "";
}

final StringBuilder builder = new StringBuilder();
for (int i = (string.length() - 1); i >= 0; i--) {
builder.append(string.charAt(i));
Expand Down