-
Code public class AbbreviateTwoWords { public static String abbrevName(String name) { return "%s.%s".formatted(name.charAt(0), name.charAt(name.indexOf(" ") + 1)); } }
Test Cases import org.junit.Test; import static org.junit.Assert.assertEquals; import org.junit.runners.JUnit4; public class SolutionTest { public void testFixed() { assertEquals("S.H", AbbreviateTwoWords.abbrevName("Sam Harris")); assertEquals("P.F", AbbreviateTwoWords.abbrevName("Patrick Feenan")); assertEquals("E.C", AbbreviateTwoWords.abbrevName("Evan Cole")); assertEquals("P.F", AbbreviateTwoWords.abbrevName("P Favuzzi")); assertEquals("D.M", AbbreviateTwoWords.abbrevName("David Mendieta")); } }
Output:
-
Code - public class AbbreviateTwoWords {
- public static String abbrevName(String name) {
return name.replaceAll("[^A-Z\\s]","").replaceAll("\\s",".");- return "%s.%s".formatted(name.charAt(0), name.charAt(name.indexOf(" ") + 1));
- }
- }
- All
- {{group.name}} ({{group.count}})
This comment has been reported as {{ abuseKindText }}.
Show
This comment has been hidden. You can view it now .
This comment can not be viewed.
- |
- Reply
- Edit
- View Solution
- Expand 1 Reply Expand {{ comments?.length }} replies
- Collapse
- Remove
- Remove comment & replies
- Report
{{ fetchSolutionsError }}