package space; public class SpaceMaker { public static String spaceMakerV1(String str) { return str.replaceAll(".(?=.)", "$0 "); } }
- package space;
import static java.util.stream.Collectors.joining;public class SpaceMaker{public static String spaceMakerV1(String str){return str.chars().mapToObj(c -> String.valueOf((char)c)).collect(joining(" "));}public static String spaceMakerV2(String str){char[] chars = str.toCharArray();StringBuilder spaced = new StringBuilder();for(char c:chars){spaced.append(c).append(' ');}return spaced.toString().trim();- public class SpaceMaker
- {
- public static String spaceMakerV1(String str)
- {
- return str.replaceAll(".(?=.)", "$0 ");
- }
- }
import static org.junit.Assert.*; import static space.SpaceMaker.spaceMakerV1; import org.junit.Test; public class SpaceMakerTest{ @Test public void testV1(){ assertEquals("o c t o b e r" , spaceMakerV1("october") ); assertEquals("s p a c e" , spaceMakerV1("space") ); assertEquals("r a n d o m" , spaceMakerV1("random") ); assertEquals("j a v a s c r i p t", spaceMakerV1("javascript")); assertEquals("c o d e w a r s" , spaceMakerV1("codewars") ); assertEquals("s t r i n g" , spaceMakerV1("string") ); } }
- import static org.junit.Assert.*;
- import static space.SpaceMaker.spaceMakerV1;
import static space.SpaceMaker.spaceMakerV2;- import org.junit.Test;
- public class SpaceMakerTest{
- @Test
- public void testV1(){
- assertEquals("o c t o b e r" , spaceMakerV1("october") );
- assertEquals("s p a c e" , spaceMakerV1("space") );
- assertEquals("r a n d o m" , spaceMakerV1("random") );
- assertEquals("j a v a s c r i p t", spaceMakerV1("javascript"));
- assertEquals("c o d e w a r s" , spaceMakerV1("codewars") );
- assertEquals("s t r i n g" , spaceMakerV1("string") );
- }
@Testpublic void testV2(){assertEquals("o c t o b e r" , spaceMakerV2("october") );assertEquals("s p a c e" , spaceMakerV2("space") );assertEquals("r a n d o m" , spaceMakerV2("random") );assertEquals("j a v a s c r i p t", spaceMakerV2("javascript"));assertEquals("c o d e w a r s" , spaceMakerV2("codewars") );assertEquals("s t r i n g" , spaceMakerV2("string") );}- }