196k views
5 votes
Write in java. No print or println. Only use return. This is a codingbat problem.

There are cultures that believe some numbers are unlucky. We want to remove all of these unlucky characters from our Strings so the user can use the Strings without the unlucky attributes. For our case, we believe that the number 17 is unlucky so we want to remove these from our Strings.

unlucky17("Th17is is a S1717tring") → "This is a String"
unlucky17("17Another 17String17") → "Another String"
unlucky17("F17i17n17a17l17l17y") → "Finally"

1 Answer

1 vote

Final answer:

A Java method called unlucky17 can be implemented to remove the sequence "17" from a given String by using the String.replace() method, which replaces all occurrences of "17" with an empty String.

Step-by-step explanation:

The problem described is related to manipulating Strings in Java to remove a certain sequence of characters. In this case, the sequence "17" should be removed from the input String. We can solve this by using the String.replace() method, which allows us to replace all occurrences of a specific sequence of characters with another sequence. In our scenario, we would replace "17" with an empty String. Below is an example of how this can be implemented in a method called unlucky17:

public String unlucky17(String str) {
return str.replace("17", "");
}

This method takes a String as an argument and returns a new String with all instances of "17" removed.