Strings
Arrays
Final solution, no given as argument a void string.
import java.text.ParseException; import java.util.ArrayList; import java.util.Arrays; import java.util.Random; public class Kata{ public static String[] replaceLetter(String str, int n) { ArrayList<String> arr = new ArrayList<>(); StringBuilder strFinal = new StringBuilder(str); int nextIndex = n - 1; int posSpaces = n; // Find first spaces for(int i =0; i<posSpaces; i++) if(str.charAt(i% str.length())==' ') { posSpaces++; nextIndex++; } // Iterate string for (int i = 0; i < str.length(); i++) { nextIndex = (nextIndex + 1) % str.length(); if (str.charAt(i) != ' ') { while (str.charAt(nextIndex) == ' ') { nextIndex = (nextIndex + 1) % str.length(); } // Replace letters strFinal.replace(i, i + 1, String.valueOf(str.charAt(nextIndex))); arr.add(strFinal.toString()); // Reset string to add strFinal = new StringBuilder(str); } else{ // current index is white space nextIndex --; } } return arr.toArray(new String[arr.size()]); } }
- import java.text.ParseException;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;- public class Kata{
public static String[] replaceLetter(String str, int n) {- public static String[] replaceLetter(String str, int n) {
ArrayList<String> arr = new ArrayList<>();StringBuilder strFinal = new StringBuilder(str);for (int i = 0; i < str.length(); i++) {int nextIndex = (i + n) % str.length();if (str.charAt(i) != ' ') {if (str.charAt(nextIndex) != ' ') {strFinal.replace(i, i + 1, String.valueOf(str.charAt(nextIndex)));arr.add(strFinal.toString());strFinal.replace(0, str.length(), str);} else {while (str.charAt(nextIndex) == ' ') {if (nextIndex == str.length() - 1) {nextIndex = 0;} else {nextIndex++;}- ArrayList<String> arr = new ArrayList<>();
- StringBuilder strFinal = new StringBuilder(str);
- int nextIndex = n - 1;
- int posSpaces = n;
- // Find first spaces
- for(int i =0; i<posSpaces; i++) if(str.charAt(i% str.length())==' ') {
- posSpaces++;
- nextIndex++;
- }
- // Iterate string
- for (int i = 0; i < str.length(); i++) {
- nextIndex = (nextIndex + 1) % str.length();
- if (str.charAt(i) != ' ') {
- while (str.charAt(nextIndex) == ' ') {
- nextIndex = (nextIndex + 1) % str.length();
- }
- // Replace letters
- strFinal.replace(i, i + 1, String.valueOf(str.charAt(nextIndex)));
- arr.add(strFinal.toString());
strFinal.replace(0, str.length(), str);- // Reset string to add
- strFinal = new StringBuilder(str);
- } else{ // current index is white space
- nextIndex --;
- }
}}- }
return arr.toArray(new String[arr.size()]);- return arr.toArray(new String[arr.size()]);
- }
- }
import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.RepeatedTest; import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; import java.util.Random; import java.util.ArrayList; class SolutionTest { @Test @Tag("BasicTest") void BasicTest() { String[] reverseLeterHello = { "lello", "hollo", "hehlo", "heleo", "helll" }; String[] reverseLeterBye = { "eye", "bbe", "byy" }; String[] reverseLeterThanks = { "Thanks", "Thanks", "Thanks", "Thanks", "Thanks", "Thanks" }; String[] reverseLetterHellowWorld = { "lello world", "Hlllo world", "Heolo world", "Helwo world", "Hello world", "Hello rorld", "Hello wlrld", "Hello wodld", "Hello worHd", "Hello worle" }; String[] reverseLetterHellowWorldLong = {" rello world "," hlllo world "," hedlo world "," helho world "," helle world "," hello lorld "," hello wlrld "," hello woold "," hello worwd "," hello worlo "}; assertArrayEquals(reverseLeterHello, Kata.replaceLetter("hello", 3)); assertArrayEquals(reverseLeterThanks, Kata.replaceLetter("Thanks", 0)); assertArrayEquals(reverseLetterHellowWorld, Kata.replaceLetter("Hello world", 2)); assertArrayEquals(reverseLetterHellowWorldLong, Kata.replaceLetter(" hello world ", 7)); } @Test @Tag("SpecialCase") void SpecialCase() { String[] reverseLeterVoid = {}; assertArrayEquals(reverseLeterVoid, Kata.replaceLetter(" ", 2)); assertArrayEquals(new String[] { " a " }, Kata.replaceLetter(" a ", 1)); assertArrayEquals(new String[] { " bb ", " aa " }, Kata.replaceLetter(" ab ", 7)); } @Tag("RandomTest") @RepeatedTest(10) @DisplayName("RandomWord") void RandomTest() { String word = generate(new Random().nextInt(15) + 1); int letterRandom = new Random().nextInt(word.length()); assertArrayEquals(replaceLetter(word, letterRandom), Kata.replaceLetter(word, letterRandom)); } public static String generate(int length) { String strChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 ^*¨_:;=?¿"; StringBuilder sb = new StringBuilder(); for (int i = 0; i < length; i++) { int nRandom = new Random().nextInt(strChars.length()); sb.append(strChars.charAt(nRandom)); } return sb.toString(); } public static String[] replaceLetter(String str, int n) { ArrayList<String> arr = new ArrayList<>(); StringBuilder strFinal = new StringBuilder(str); int nextIndex = n - 1; int posSpaces = n; // Find first spaces for(int i =0; i<posSpaces; i++) if(str.charAt(i% str.length())==' ') { posSpaces++; nextIndex++; } // Iterate string for (int i = 0; i < str.length(); i++) { nextIndex = (nextIndex + 1) % str.length(); if (str.charAt(i) != ' ') { while (str.charAt(nextIndex) == ' ') { nextIndex = (nextIndex + 1) % str.length(); } // Replace letters strFinal.replace(i, i + 1, String.valueOf(str.charAt(nextIndex))); arr.add(strFinal.toString()); // Reset string to add strFinal = new StringBuilder(str); } else{ // current index is white space nextIndex --; } } return arr.toArray(new String[arr.size()]); } }
- import static org.junit.jupiter.api.Assertions.*;
- import org.junit.jupiter.api.DisplayName;
- import org.junit.jupiter.api.RepeatedTest;
- import org.junit.jupiter.api.Tag;
- import org.junit.jupiter.api.Test;
- import java.util.Random;
- import java.util.ArrayList;
// TODO: Replace examples and use TDD by writing your own tests- class SolutionTest {
- @Test
- @Tag("BasicTest")
- void BasicTest() {
- String[] reverseLeterHello = { "lello", "hollo", "hehlo", "heleo", "helll" };
- String[] reverseLeterBye = { "eye", "bbe", "byy" };
- String[] reverseLeterThanks = { "Thanks", "Thanks", "Thanks", "Thanks", "Thanks", "Thanks" };
String[] reverseLetterHellowWorld = { "eello world", "Hlllo world", "Hello world", "Heloo world", "Hellw world", "Hello oorld", "Hello wrrld", "Hello wolld", "Hello wordd", "Hello worlH" };- String[] reverseLetterHellowWorld = { "lello world", "Hlllo world", "Heolo world", "Helwo world",
- "Hello world", "Hello rorld", "Hello wlrld", "Hello wodld", "Hello worHd", "Hello worle" };
- String[] reverseLetterHellowWorldLong = {" rello world "," hlllo world "," hedlo world "," helho world "," helle world "," hello lorld "," hello wlrld "," hello woold "," hello worwd "," hello worlo "};
- assertArrayEquals(reverseLeterHello, Kata.replaceLetter("hello", 3));
assertArrayEquals(reverseLeterBye, Kata.replaceLetter("bye", 2));- assertArrayEquals(reverseLeterThanks, Kata.replaceLetter("Thanks", 0));
assertArrayEquals(reverseLetterHellowWorld, Kata.replaceLetter("Hello world", 1));- assertArrayEquals(reverseLetterHellowWorld, Kata.replaceLetter("Hello world", 2));
- assertArrayEquals(reverseLetterHellowWorldLong, Kata.replaceLetter(" hello world ", 7));
- }
- @Test
- @Tag("SpecialCase")
- void SpecialCase() {
- String[] reverseLeterVoid = {};
assertArrayEquals(reverseLeterVoid, Kata.replaceLetter("", 2));- assertArrayEquals(reverseLeterVoid, Kata.replaceLetter(" ", 2));
assertArrayEquals(reverseLeterVoid, Kata.replaceLetter(" ", 0));- assertArrayEquals(new String[] { " a " }, Kata.replaceLetter(" a ", 1));
- assertArrayEquals(new String[] { " bb ", " aa " }, Kata.replaceLetter(" ab ", 7));
- }
- @Tag("RandomTest")
@RepeatedTest(100)- @RepeatedTest(10)
- @DisplayName("RandomWord")
- void RandomTest() {
- String word = generate(new Random().nextInt(15) + 1);
- int letterRandom = new Random().nextInt(word.length());
- assertArrayEquals(replaceLetter(word, letterRandom), Kata.replaceLetter(word, letterRandom));
- }
- public static String generate(int length) {
- String strChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 ^*¨_:;=?¿";
- StringBuilder sb = new StringBuilder();
- for (int i = 0; i < length; i++) {
- int nRandom = new Random().nextInt(strChars.length());
- sb.append(strChars.charAt(nRandom));
- }
- return sb.toString();
- }
public static String[] replaceLetter(String str, int n) {ArrayList<String> arr = new ArrayList<>();StringBuilder strFinal = new StringBuilder(str);for (int i = 0; i < str.length(); i++) {int nextIndex = (i + n) % str.length();if (str.charAt(i) != ' ') {if (str.charAt(nextIndex) != ' ') {strFinal.replace(i, i + 1, String.valueOf(str.charAt(nextIndex)));arr.add(strFinal.toString());strFinal.replace(0, str.length(), str);- public static String[] replaceLetter(String str, int n) {
} else {while (str.charAt(nextIndex) == ' ') {if (nextIndex == str.length() - 1) {nextIndex = 0;} else {nextIndex++;}- ArrayList<String> arr = new ArrayList<>();
- StringBuilder strFinal = new StringBuilder(str);
- int nextIndex = n - 1;
- int posSpaces = n;
- // Find first spaces
- for(int i =0; i<posSpaces; i++) if(str.charAt(i% str.length())==' ') {
- posSpaces++;
- nextIndex++;
- }
- // Iterate string
- for (int i = 0; i < str.length(); i++) {
- nextIndex = (nextIndex + 1) % str.length();
- if (str.charAt(i) != ' ') {
- while (str.charAt(nextIndex) == ' ') {
- nextIndex = (nextIndex + 1) % str.length();
- }
- // Replace letters
- strFinal.replace(i, i + 1, String.valueOf(str.charAt(nextIndex)));
- arr.add(strFinal.toString());
strFinal.replace(0, str.length(), str);- // Reset string to add
- strFinal = new StringBuilder(str);
- } else{ // current index is white space
- nextIndex --;
- }
}}- }
return arr.toArray(new String[arr.size()]);- return arr.toArray(new String[arr.size()]);
- }
- }
Strings
Arrays
import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.RepeatedTest; import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; import java.util.Random; import java.util.ArrayList; class SolutionTest { @Test @Tag("BasicTest") void BasicTest() { String[] reverseLetterHellowWorld = { "eello world", "Hlllo world", "Hello world", "Heloo world", "Hellw world", "Hello oorld", "Hello wrrld", "Hello wolld", "Hello wordd", "Hello worlH" }; assertArrayEquals(reverseLetterHellowWorld, Kata.replaceLetter("Hello world", 1)); String[] reverseLeterHello = { "lello", "hollo", "hehlo", "heleo", "helll" }; assertArrayEquals(reverseLeterHello, Kata.replaceLetter("hello", 3)); String[] reverseLeterBye = { "eye", "bbe", "byy" }; assertArrayEquals(reverseLeterBye, Kata.replaceLetter("bye", 2)); String[] reverseLeterThanks = { "Thanks", "Thanks", "Thanks", "Thanks", "Thanks", "Thanks" }; assertArrayEquals(reverseLeterThanks, Kata.replaceLetter("Thanks", 0)); String[] reverseLetterWhiteSpaceFinal = { "SSs " , "Mss " , "MSM " }; assertArrayEquals(reverseLetterWhiteSpaceFinal, Kata.replaceLetter("MSs ", 1)); } @Test @Tag("SpecialCase") void SpecialCase() { String[] reverseLeterVoid = {}; assertArrayEquals(reverseLeterVoid, Kata.replaceLetter("", 2)); assertArrayEquals(reverseLeterVoid, Kata.replaceLetter(" ", 2)); assertArrayEquals(reverseLeterVoid, Kata.replaceLetter(" ", 0)); } @Tag("RandomTest") @RepeatedTest(10) @DisplayName("RandomWord") void RandomTest() { String word = generate(new Random().nextInt(15) + 1); int letterRandom = new Random().nextInt(word.length()); assertArrayEquals(replaceLetter(word, letterRandom), Kata.replaceLetter(word, letterRandom)); } public static String generate(int length) { String strChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 ^*¨_:;=?¿"; StringBuilder sb = new StringBuilder(); for (int i = 0; i < length; i++) { int nRandom = new Random().nextInt(strChars.length()); sb.append(strChars.charAt(nRandom)); } return sb.toString(); } private static String[] replaceLetter(String str, int n) { ArrayList<String> arr = new ArrayList<>(); StringBuilder strFinal = new StringBuilder(str); for (int i = 0; i < str.length(); i++) { int nextIndex = (i + n) % str.length(); if (str.charAt(i) != ' ') { if (str.charAt(nextIndex) != ' ') { strFinal.replace(i, i + 1, String.valueOf(str.charAt(nextIndex))); arr.add(strFinal.toString()); strFinal.replace(0, str.length(), str); } else { while (str.charAt(nextIndex) == ' ') { if (nextIndex == str.length() - 1) { nextIndex = 0; } else { nextIndex++; } } strFinal.replace(i, i + 1, String.valueOf(str.charAt(nextIndex))); arr.add(strFinal.toString()); strFinal.replace(0, str.length(), str); } } } return arr.toArray(new String[arr.size()]); } }
- import static org.junit.jupiter.api.Assertions.*;
- import org.junit.jupiter.api.DisplayName;
- import org.junit.jupiter.api.RepeatedTest;
- import org.junit.jupiter.api.Tag;
- import org.junit.jupiter.api.Test;
- import java.util.Random;
- import java.util.ArrayList;
- class SolutionTest {
- @Test
- @Tag("BasicTest")
- void BasicTest() {
- String[] reverseLetterHellowWorld = { "eello world", "Hlllo world", "Hello world", "Heloo world", "Hellw world", "Hello oorld", "Hello wrrld", "Hello wolld", "Hello wordd", "Hello worlH" };
- assertArrayEquals(reverseLetterHellowWorld, Kata.replaceLetter("Hello world", 1));
- String[] reverseLeterHello = { "lello", "hollo", "hehlo", "heleo", "helll" };
- assertArrayEquals(reverseLeterHello, Kata.replaceLetter("hello", 3));
- String[] reverseLeterBye = { "eye", "bbe", "byy" };
- assertArrayEquals(reverseLeterBye, Kata.replaceLetter("bye", 2));
- String[] reverseLeterThanks = { "Thanks", "Thanks", "Thanks", "Thanks", "Thanks", "Thanks" };
- assertArrayEquals(reverseLeterThanks, Kata.replaceLetter("Thanks", 0));
- String[] reverseLetterWhiteSpaceFinal = { "SSs " , "Mss " , "MSM " };
- assertArrayEquals(reverseLetterWhiteSpaceFinal, Kata.replaceLetter("MSs ", 1));
- }
- @Test
- @Tag("SpecialCase")
- void SpecialCase() {
- String[] reverseLeterVoid = {};
- assertArrayEquals(reverseLeterVoid, Kata.replaceLetter("", 2));
- assertArrayEquals(reverseLeterVoid, Kata.replaceLetter(" ", 2));
- assertArrayEquals(reverseLeterVoid, Kata.replaceLetter(" ", 0));
- }
- @Tag("RandomTest")
- @RepeatedTest(10)
- @DisplayName("RandomWord")
- void RandomTest() {
- String word = generate(new Random().nextInt(15) + 1);
- int letterRandom = new Random().nextInt(word.length());
- assertArrayEquals(replaceLetter(word, letterRandom), Kata.replaceLetter(word, letterRandom));
- }
- public static String generate(int length) {
- String strChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 ^*¨_:;=?¿";
- StringBuilder sb = new StringBuilder();
- for (int i = 0; i < length; i++) {
- int nRandom = new Random().nextInt(strChars.length());
- sb.append(strChars.charAt(nRandom));
- }
- return sb.toString();
- }
public static String[] replaceLetter(String str, int n) {- private static String[] replaceLetter(String str, int n) {
- ArrayList<String> arr = new ArrayList<>();
- StringBuilder strFinal = new StringBuilder(str);
- for (int i = 0; i < str.length(); i++) {
- int nextIndex = (i + n) % str.length();
- if (str.charAt(i) != ' ') {
- if (str.charAt(nextIndex) != ' ') {
- strFinal.replace(i, i + 1, String.valueOf(str.charAt(nextIndex)));
- arr.add(strFinal.toString());
- strFinal.replace(0, str.length(), str);
- } else {
- while (str.charAt(nextIndex) == ' ') {
- if (nextIndex == str.length() - 1) {
- nextIndex = 0;
- } else {
- nextIndex++;
- }
- }
- strFinal.replace(i, i + 1, String.valueOf(str.charAt(nextIndex)));
- arr.add(strFinal.toString());
- strFinal.replace(0, str.length(), str);
- }
- }
- }
- return arr.toArray(new String[arr.size()]);
- }
- }
Strings
Arrays
import java.text.ParseException; import java.util.ArrayList; import java.util.Arrays; import java.util.Random; import java.util.concurrent.ThreadLocalRandom; public class Kata{ public static String[] replaceLetter(String str, int n) { ArrayList<String> arr = new ArrayList<>(); StringBuilder strFinal = new StringBuilder(str); for (int i = 0; i < str.length(); i++) { int nextIndex = (i + n) % str.length(); if (str.charAt(i) != ' ') { if (str.charAt(nextIndex) != ' ') { strFinal.replace(i, i + 1, String.valueOf(str.charAt(nextIndex))); arr.add(strFinal.toString()); strFinal.replace(0, str.length(), str); } else { while (str.charAt(nextIndex) == ' ') { if (nextIndex == str.length() - 1) { nextIndex = 0; } else { nextIndex++; } } strFinal.replace(i, i + 1, String.valueOf(str.charAt(nextIndex))); arr.add(strFinal.toString()); strFinal.replace(0, str.length(), str); } } } return arr.toArray(new String[arr.size()]); } }
- import java.text.ParseException;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Random;
- import java.util.concurrent.ThreadLocalRandom;
- public class Kata{
- public static String[] replaceLetter(String str, int n) {
- ArrayList<String> arr = new ArrayList<>();
- StringBuilder strFinal = new StringBuilder(str);
- for (int i = 0; i < str.length(); i++) {
int nextIndex = (i + n) % str.length();- int nextIndex = (i + n) % str.length();
- if (str.charAt(i) != ' ') {
- if (str.charAt(nextIndex) != ' ') {
- strFinal.replace(i, i + 1, String.valueOf(str.charAt(nextIndex)));
- arr.add(strFinal.toString());
- strFinal.replace(0, str.length(), str);
- } else {
- while (str.charAt(nextIndex) == ' ') {
nextIndex++;- if (nextIndex == str.length() - 1) {
- nextIndex = 0;
- } else {
- nextIndex++;
- }
- }
- strFinal.replace(i, i + 1, String.valueOf(str.charAt(nextIndex)));
- arr.add(strFinal.toString());
- strFinal.replace(0, str.length(), str);
- }
- }
- }
- return arr.toArray(new String[arr.size()]);
- }
- }
import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.RepeatedTest; import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; import java.util.Random; import java.util.ArrayList; class SolutionTest { @Test @Tag("BasicTest") void BasicTest() { String[] reverseLetterHellowWorld = { "eello world", "Hlllo world", "Hello world", "Heloo world", "Hellw world", "Hello oorld", "Hello wrrld", "Hello wolld", "Hello wordd", "Hello worlH" }; assertArrayEquals(reverseLetterHellowWorld, Kata.replaceLetter("Hello world", 1)); String[] reverseLeterHello = { "lello", "hollo", "hehlo", "heleo", "helll" }; assertArrayEquals(reverseLeterHello, Kata.replaceLetter("hello", 3)); String[] reverseLeterBye = { "eye", "bbe", "byy" }; assertArrayEquals(reverseLeterBye, Kata.replaceLetter("bye", 2)); String[] reverseLeterThanks = { "Thanks", "Thanks", "Thanks", "Thanks", "Thanks", "Thanks" }; assertArrayEquals(reverseLeterThanks, Kata.replaceLetter("Thanks", 0)); String[] reverseLetterWhiteSpaceFinal = { "SSs " , "Mss " , "MSM " }; assertArrayEquals(reverseLetterWhiteSpaceFinal, Kata.replaceLetter("MSs ", 1)); } @Test @Tag("SpecialCase") void SpecialCase() { String[] reverseLeterVoid = {}; assertArrayEquals(reverseLeterVoid, Kata.replaceLetter("", 2)); assertArrayEquals(reverseLeterVoid, Kata.replaceLetter(" ", 2)); assertArrayEquals(reverseLeterVoid, Kata.replaceLetter(" ", 0)); } @Tag("RandomTest") @RepeatedTest(10) @DisplayName("RandomWord") void RandomTest() { String word = generate(new Random().nextInt(15) + 1); int letterRandom = new Random().nextInt(word.length()); assertArrayEquals(replaceLetter(word, letterRandom), Kata.replaceLetter(word, letterRandom)); } public static String generate(int length) { String strChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 ^*¨_:;=?¿"; StringBuilder sb = new StringBuilder(); for (int i = 0; i < length; i++) { int nRandom = new Random().nextInt(strChars.length()); sb.append(strChars.charAt(nRandom)); } return sb.toString(); } public static String[] replaceLetter(String str, int n) { ArrayList<String> arr = new ArrayList<>(); StringBuilder strFinal = new StringBuilder(str); for (int i = 0; i < str.length(); i++) { int nextIndex = (i + n) % str.length(); if (str.charAt(i) != ' ') { if (str.charAt(nextIndex) != ' ') { strFinal.replace(i, i + 1, String.valueOf(str.charAt(nextIndex))); arr.add(strFinal.toString()); strFinal.replace(0, str.length(), str); } else { while (str.charAt(nextIndex) == ' ') { if (nextIndex == str.length() - 1) { nextIndex = 0; } else { nextIndex++; } } strFinal.replace(i, i + 1, String.valueOf(str.charAt(nextIndex))); arr.add(strFinal.toString()); strFinal.replace(0, str.length(), str); } } } return arr.toArray(new String[arr.size()]); } }
- import static org.junit.jupiter.api.Assertions.*;
- import org.junit.jupiter.api.DisplayName;
- import org.junit.jupiter.api.RepeatedTest;
- import org.junit.jupiter.api.Tag;
- import org.junit.jupiter.api.Test;
- import java.util.Random;
- import java.util.ArrayList;
// TODO: Replace examples and use TDD by writing your own tests- class SolutionTest {
- @Test
- @Tag("BasicTest")
- void BasicTest() {
- String[] reverseLetterHellowWorld = { "eello world", "Hlllo world", "Hello world", "Heloo world", "Hellw world", "Hello oorld", "Hello wrrld", "Hello wolld", "Hello wordd", "Hello worlH" };
- assertArrayEquals(reverseLetterHellowWorld, Kata.replaceLetter("Hello world", 1));
- String[] reverseLeterHello = { "lello", "hollo", "hehlo", "heleo", "helll" };
- assertArrayEquals(reverseLeterHello, Kata.replaceLetter("hello", 3));
- String[] reverseLeterBye = { "eye", "bbe", "byy" };
- assertArrayEquals(reverseLeterBye, Kata.replaceLetter("bye", 2));
- String[] reverseLeterThanks = { "Thanks", "Thanks", "Thanks", "Thanks", "Thanks", "Thanks" };
- assertArrayEquals(reverseLeterThanks, Kata.replaceLetter("Thanks", 0));
- String[] reverseLetterWhiteSpaceFinal = { "SSs " , "Mss " , "MSM " };
- assertArrayEquals(reverseLetterWhiteSpaceFinal, Kata.replaceLetter("MSs ", 1));
- }
- @Test
- @Tag("SpecialCase")
- void SpecialCase() {
- String[] reverseLeterVoid = {};
- assertArrayEquals(reverseLeterVoid, Kata.replaceLetter("", 2));
- assertArrayEquals(reverseLeterVoid, Kata.replaceLetter(" ", 2));
- assertArrayEquals(reverseLeterVoid, Kata.replaceLetter(" ", 0));
- }
- @Tag("RandomTest")
- @RepeatedTest(10)
- @DisplayName("RandomWord")
- void RandomTest() {
- String word = generate(new Random().nextInt(15) + 1);
- int letterRandom = new Random().nextInt(word.length());
- assertArrayEquals(replaceLetter(word, letterRandom), Kata.replaceLetter(word, letterRandom));
- }
- public static String generate(int length) {
- String strChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 ^*¨_:;=?¿";
- StringBuilder sb = new StringBuilder();
- for (int i = 0; i < length; i++) {
- int nRandom = new Random().nextInt(strChars.length());
- sb.append(strChars.charAt(nRandom));
- }
- return sb.toString();
- }
- public static String[] replaceLetter(String str, int n) {
- ArrayList<String> arr = new ArrayList<>();
- StringBuilder strFinal = new StringBuilder(str);
- for (int i = 0; i < str.length(); i++) {
- int nextIndex = (i + n) % str.length();
- if (str.charAt(i) != ' ') {
- if (str.charAt(nextIndex) != ' ') {
- strFinal.replace(i, i + 1, String.valueOf(str.charAt(nextIndex)));
- arr.add(strFinal.toString());
- strFinal.replace(0, str.length(), str);
- } else {
- while (str.charAt(nextIndex) == ' ') {
nextIndex++;- if (nextIndex == str.length() - 1) {
- nextIndex = 0;
- } else {
- nextIndex++;
- }
- }
- strFinal.replace(i, i + 1, String.valueOf(str.charAt(nextIndex)));
- arr.add(strFinal.toString());
- strFinal.replace(0, str.length(), str);
- }
- }
- }
- return arr.toArray(new String[arr.size()]);
- }
- }
Strings
Arrays
Arreglado el problema de los espacios DEFINITIVAMENTE, añadido test
import java.text.ParseException; import java.util.ArrayList; import java.util.Arrays; import java.util.Random; import java.util.concurrent.ThreadLocalRandom; public class Kata{ public static String[] replaceLetter(String str, int n) { ArrayList<String> arr = new ArrayList<>(); StringBuilder strFinal = new StringBuilder(str); for (int i = 0; i < str.length(); i++) { int nextIndex = (i + n) % str.length(); if (str.charAt(i) != ' ') { if (str.charAt(nextIndex) != ' ') { strFinal.replace(i, i + 1, String.valueOf(str.charAt(nextIndex))); arr.add(strFinal.toString()); strFinal.replace(0, str.length(), str); } else { while (str.charAt(nextIndex) == ' ') { nextIndex++; } strFinal.replace(i, i + 1, String.valueOf(str.charAt(nextIndex))); arr.add(strFinal.toString()); strFinal.replace(0, str.length(), str); } } } return arr.toArray(new String[arr.size()]); } }
- import java.text.ParseException;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Random;
- import java.util.concurrent.ThreadLocalRandom;
- public class Kata{
public static String[] replaceLetter(String str, int n) {- public static String[] replaceLetter(String str, int n) {
- ArrayList<String> arr = new ArrayList<>();
- StringBuilder strFinal = new StringBuilder(str);
- for (int i = 0; i < str.length(); i++) {
int nextIndex = (i + n) % str.length() ;- int nextIndex = (i + n) % str.length();
- if (str.charAt(i) != ' ') {
if (str.charAt(i) != ' ' && str.charAt(nextIndex) != ' ') {- if (str.charAt(nextIndex) != ' ') {
nextIndex = (i + n) % str.length();strFinal.replace(i, i + 1, String.valueOf(str.charAt(nextIndex)));arr.add(strFinal.toString());strFinal.replace(0, str.length(), str);- strFinal.replace(i, i + 1, String.valueOf(str.charAt(nextIndex)));
- arr.add(strFinal.toString());
- strFinal.replace(0, str.length(), str);
- } else {
- while (str.charAt(nextIndex) == ' ') {
- nextIndex++;
- }
- strFinal.replace(i, i + 1, String.valueOf(str.charAt(nextIndex)));
- arr.add(strFinal.toString());
- strFinal.replace(0, str.length(), str);
- }
- }
- }
- return arr.toArray(new String[arr.size()]);
- }
- }
import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.RepeatedTest; import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; import java.util.Random; import java.util.ArrayList; // TODO: Replace examples and use TDD by writing your own tests class SolutionTest { @Test @Tag("BasicTest") void BasicTest() { String[] reverseLeterHello = { "lello", "hollo", "hehlo", "heleo", "helll" }; String[] reverseLeterBye = { "eye", "bbe", "byy" }; String[] reverseLeterThanks = { "Thanks", "Thanks", "Thanks", "Thanks", "Thanks", "Thanks" }; String[] reverseLetterHellowWorld = { "eello world", "Hlllo world", "Hello world", "Heloo world", "Hellw world", "Hello oorld", "Hello wrrld", "Hello wolld", "Hello wordd", "Hello worlH" }; assertArrayEquals(reverseLeterHello, Kata.replaceLetter("hello", 3)); assertArrayEquals(reverseLeterBye, Kata.replaceLetter("bye", 2)); assertArrayEquals(reverseLeterThanks, Kata.replaceLetter("Thanks", 0)); assertArrayEquals(reverseLetterHellowWorld, Kata.replaceLetter("Hello world", 1)); } @Test @Tag("SpecialCase") void SpecialCase() { String[] reverseLeterVoid = {}; assertArrayEquals(reverseLeterVoid, Kata.replaceLetter("", 2)); assertArrayEquals(reverseLeterVoid, Kata.replaceLetter(" ", 2)); assertArrayEquals(reverseLeterVoid, Kata.replaceLetter(" ", 0)); } @Tag("RandomTest") @RepeatedTest(10) @DisplayName("RandomWord") void RandomTest() { String word = generate(new Random().nextInt(15) + 1); int letterRandom = new Random().nextInt(word.length()); assertArrayEquals(replaceLetter(word, letterRandom), Kata.replaceLetter(word, letterRandom)); } public static String generate(int length) { String strChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 ^*¨_:;=?¿"; StringBuilder sb = new StringBuilder(); for (int i = 0; i < length; i++) { int nRandom = new Random().nextInt(strChars.length()); sb.append(strChars.charAt(nRandom)); } return sb.toString(); } public static String[] replaceLetter(String str, int n) { ArrayList<String> arr = new ArrayList<>(); StringBuilder strFinal = new StringBuilder(str); for (int i = 0; i < str.length(); i++) { int nextIndex = (i + n) % str.length(); if (str.charAt(i) != ' ') { if (str.charAt(nextIndex) != ' ') { strFinal.replace(i, i + 1, String.valueOf(str.charAt(nextIndex))); arr.add(strFinal.toString()); strFinal.replace(0, str.length(), str); } else { while (str.charAt(nextIndex) == ' ') { nextIndex++; } strFinal.replace(i, i + 1, String.valueOf(str.charAt(nextIndex))); arr.add(strFinal.toString()); strFinal.replace(0, str.length(), str); } } } return arr.toArray(new String[arr.size()]); } }
- import static org.junit.jupiter.api.Assertions.*;
- import org.junit.jupiter.api.DisplayName;
- import org.junit.jupiter.api.RepeatedTest;
- import org.junit.jupiter.api.Tag;
- import org.junit.jupiter.api.Test;
- import java.util.Random;
- import java.util.ArrayList;
- // TODO: Replace examples and use TDD by writing your own tests
- class SolutionTest {
- @Test
- @Tag("BasicTest")
- void BasicTest() {
- String[] reverseLeterHello = { "lello", "hollo", "hehlo", "heleo", "helll" };
- String[] reverseLeterBye = { "eye", "bbe", "byy" };
String[] reverseLeterCodewars = { "oello codewars", "hdllo codewars", "heelo codewars", "helwo codewars","hella codewars", "hello sodewars", "hello chdewars", "hello coeewars", "hello codlwars","hello codelars", "hello codewors", "hello codewarc" };- String[] reverseLeterThanks = { "Thanks", "Thanks", "Thanks", "Thanks", "Thanks", "Thanks" };
- String[] reverseLetterHellowWorld = { "eello world", "Hlllo world", "Hello world", "Heloo world", "Hellw world", "Hello oorld", "Hello wrrld", "Hello wolld", "Hello wordd", "Hello worlH" };
- assertArrayEquals(reverseLeterHello, Kata.replaceLetter("hello", 3));
- assertArrayEquals(reverseLeterBye, Kata.replaceLetter("bye", 2));
assertArrayEquals(reverseLeterCodewars, Kata.replaceLetter("hello codewars", 7));- assertArrayEquals(reverseLeterThanks, Kata.replaceLetter("Thanks", 0));
- assertArrayEquals(reverseLetterHellowWorld, Kata.replaceLetter("Hello world", 1));
- }
- @Test
- @Tag("SpecialCase")
- void SpecialCase() {
- String[] reverseLeterVoid = {};
- assertArrayEquals(reverseLeterVoid, Kata.replaceLetter("", 2));
- assertArrayEquals(reverseLeterVoid, Kata.replaceLetter(" ", 2));
- assertArrayEquals(reverseLeterVoid, Kata.replaceLetter(" ", 0));
- }
- @Tag("RandomTest")
- @RepeatedTest(10)
- @DisplayName("RandomWord")
- void RandomTest() {
- String word = generate(new Random().nextInt(15) + 1);
- int letterRandom = new Random().nextInt(word.length());
- assertArrayEquals(replaceLetter(word, letterRandom), Kata.replaceLetter(word, letterRandom));
- }
- public static String generate(int length) {
- String strChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 ^*¨_:;=?¿";
- StringBuilder sb = new StringBuilder();
- for (int i = 0; i < length; i++) {
- int nRandom = new Random().nextInt(strChars.length());
- sb.append(strChars.charAt(nRandom));
- }
- return sb.toString();
- }
public static String[] replaceLetter(String str, int n) {- public static String[] replaceLetter(String str, int n) {
- ArrayList<String> arr = new ArrayList<>();
- StringBuilder strFinal = new StringBuilder(str);
- for (int i = 0; i < str.length(); i++) {
- int nextIndex = (i + n) % str.length();
- if (str.charAt(i) != ' ') {
- if (str.charAt(nextIndex) != ' ') {
if (str.charAt(i) != ' ' && str.charAt(nextIndex) != ' ') {nextIndex = (i + n) % str.length();strFinal.replace(i, i + 1, String.valueOf(str.charAt(nextIndex)));arr.add(strFinal.toString());strFinal.replace(0, str.length(), str);- strFinal.replace(i, i + 1, String.valueOf(str.charAt(nextIndex)));
- arr.add(strFinal.toString());
- strFinal.replace(0, str.length(), str);
- } else {
- while (str.charAt(nextIndex) == ' ') {
- nextIndex++;
- }
- strFinal.replace(i, i + 1, String.valueOf(str.charAt(nextIndex)));
- arr.add(strFinal.toString());
- strFinal.replace(0, str.length(), str);
- }
- }
- }
String[] array = arr.toArray(new String[arr.size()]);return array;- return arr.toArray(new String[arr.size()]);
- }
- }
Strings
Last Tests add
import java.text.ParseException; import java.util.ArrayList; import java.util.Arrays; import java.util.Random; import java.util.concurrent.ThreadLocalRandom; public class Kata{ public static String[] replaceLetter(String str, int n) { ArrayList<String> arr = new ArrayList<>(); StringBuilder strFinal = new StringBuilder(str); for (int i = 0; i < str.length(); i++) { int nextIndex = (i + n) % str.length() ; if (str.charAt(i) != ' ' && str.charAt(nextIndex) != ' ') { nextIndex = (i + n) % str.length(); strFinal.replace(i, i + 1, String.valueOf(str.charAt(nextIndex))); arr.add(strFinal.toString()); strFinal.replace(0, str.length(), str); } } String[] array = arr.toArray(new String[arr.size()]); return array; } }
- import java.text.ParseException;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Random;
- import java.util.concurrent.ThreadLocalRandom;
- public class Kata{
- public static String[] replaceLetter(String str, int n) {
- ArrayList<String> arr = new ArrayList<>();
- StringBuilder strFinal = new StringBuilder(str);
- for (int i = 0; i < str.length(); i++) {
- int nextIndex = (i + n) % str.length() ;
- if (str.charAt(i) != ' ' && str.charAt(nextIndex) != ' ') {
- nextIndex = (i + n) % str.length();
- strFinal.replace(i, i + 1, String.valueOf(str.charAt(nextIndex)));
- arr.add(strFinal.toString());
- strFinal.replace(0, str.length(), str);
- }
- }
System.out.println(arr.toString());- String[] array = arr.toArray(new String[arr.size()]);
- return array;
- }
- }
import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.RepeatedTest; import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; import java.util.Random; import java.util.ArrayList; // TODO: Replace examples and use TDD by writing your own tests class SolutionTest { @Test @Tag("BasicTest") void BasicTest() { String[] reverseLeterHello = { "lello", "hollo", "hehlo", "heleo", "helll" }; String[] reverseLeterBye = { "eye", "bbe", "byy" }; String[] reverseLeterCodewars = { "oello codewars", "hdllo codewars", "heelo codewars", "helwo codewars", "hella codewars", "hello sodewars", "hello chdewars", "hello coeewars", "hello codlwars", "hello codelars", "hello codewors", "hello codewarc" }; String[] reverseLeterThanks = { "Thanks", "Thanks", "Thanks", "Thanks", "Thanks", "Thanks" }; assertArrayEquals(reverseLeterHello, Kata.replaceLetter("hello", 3)); assertArrayEquals(reverseLeterBye, Kata.replaceLetter("bye", 2)); assertArrayEquals(reverseLeterCodewars, Kata.replaceLetter("hello codewars", 7)); assertArrayEquals(reverseLeterThanks, Kata.replaceLetter("Thanks", 0)); } @Test @Tag("SpecialCase") void SpecialCase() { String[] reverseLeterVoid = {}; assertArrayEquals(reverseLeterVoid, Kata.replaceLetter("", 2)); assertArrayEquals(reverseLeterVoid, Kata.replaceLetter(" ", 2)); } @Test @Tag("UnitTest") void parseExceptionFecha() { assertThrows(NullPointerException.class, () -> { int nRandom = new Random().nextInt(15)+1; Kata.replaceLetter(null, nRandom); }); } @Tag("RandomTest") @RepeatedTest(10) @DisplayName("RandomWord") void RandomTest() { String word = generate(new Random().nextInt(15) + 1); int letterRandom = new Random().nextInt(word.length()); assertArrayEquals(replaceLetter(word, letterRandom), Kata.replaceLetter(word, letterRandom)); } public static String generate(int length) { String strChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; StringBuilder sb = new StringBuilder(); for (int i = 0; i < length; i++) { int nRandom = new Random().nextInt(strChars.length()); sb.append(strChars.charAt(nRandom)); } return sb.toString(); } public static String[] replaceLetter(String str, int n) { ArrayList<String> arr = new ArrayList<>(); StringBuilder strFinal = new StringBuilder(str); for (int i = 0; i < str.length(); i++) { int nextIndex = (i + n) % str.length(); if (str.charAt(i) != ' ' && str.charAt(nextIndex) != ' ') { nextIndex = (i + n) % str.length(); strFinal.replace(i, i + 1, String.valueOf(str.charAt(nextIndex))); arr.add(strFinal.toString()); strFinal.replace(0, str.length(), str); } } String[] array = arr.toArray(new String[arr.size()]); return array; } }
- import static org.junit.jupiter.api.Assertions.*;
- import org.junit.jupiter.api.DisplayName;
- import org.junit.jupiter.api.RepeatedTest;
- import org.junit.jupiter.api.Tag;
- import org.junit.jupiter.api.Test;
- import java.util.Random;
- import java.util.ArrayList;
- // TODO: Replace examples and use TDD by writing your own tests
- class SolutionTest {
- @Test
- @Tag("BasicTest")
- void BasicTest() {
String [] reverseLeterHello = {"lello", "hollo", "hehlo", "heleo", "helll"};String [] reverseLeterBye = {"eye", "bbe", "byy"};String [] reverseLeterCodewars = {"oello codewars", "hdllo codewars", "heelo codewars", "helwo codewars", "hella codewars", "hello sodewars", "hello chdewars", "hello coeewars", "hello codlwars", "hello codelars", "hello codewors", "hello codewarc"};- String[] reverseLeterHello = { "lello", "hollo", "hehlo", "heleo", "helll" };
- String[] reverseLeterBye = { "eye", "bbe", "byy" };
- String[] reverseLeterCodewars = { "oello codewars", "hdllo codewars", "heelo codewars", "helwo codewars",
- "hella codewars", "hello sodewars", "hello chdewars", "hello coeewars", "hello codlwars",
- "hello codelars", "hello codewors", "hello codewarc" };
- String[] reverseLeterThanks = { "Thanks", "Thanks", "Thanks", "Thanks", "Thanks", "Thanks" };
- assertArrayEquals(reverseLeterHello, Kata.replaceLetter("hello", 3));
- assertArrayEquals(reverseLeterBye, Kata.replaceLetter("bye", 2));
- assertArrayEquals(reverseLeterCodewars, Kata.replaceLetter("hello codewars", 7));
- assertArrayEquals(reverseLeterThanks, Kata.replaceLetter("Thanks", 0));
- }
- @Test
- @Tag("SpecialCase")
- void SpecialCase() {
- String[] reverseLeterVoid = {};
- assertArrayEquals(reverseLeterVoid, Kata.replaceLetter("", 2));
- assertArrayEquals(reverseLeterVoid, Kata.replaceLetter(" ", 2));
- }
- @Test
- @Tag("UnitTest")
- void parseExceptionFecha() {
- assertThrows(NullPointerException.class, () -> {
- int nRandom = new Random().nextInt(15)+1;
- Kata.replaceLetter(null, nRandom);
- });
- }
- @Tag("RandomTest")
- @RepeatedTest(10)
- @DisplayName("RandomWord")
- void RandomTest() {
- String word = generate(new Random().nextInt(15) + 1);
- int letterRandom = new Random().nextInt(word.length());
- assertArrayEquals(replaceLetter(word, letterRandom), Kata.replaceLetter(word, letterRandom));
- }
- public static String generate(int length) {
- String strChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
- StringBuilder sb = new StringBuilder();
- for (int i = 0; i < length; i++) {
- int nRandom = new Random().nextInt(strChars.length());
- sb.append(strChars.charAt(nRandom));
- }
- return sb.toString();
- }
- public static String[] replaceLetter(String str, int n) {
- ArrayList<String> arr = new ArrayList<>();
- StringBuilder strFinal = new StringBuilder(str);
- for (int i = 0; i < str.length(); i++) {
- int nextIndex = (i + n) % str.length();
- if (str.charAt(i) != ' ' && str.charAt(nextIndex) != ' ') {
- nextIndex = (i + n) % str.length();
- strFinal.replace(i, i + 1, String.valueOf(str.charAt(nextIndex)));
- arr.add(strFinal.toString());
- strFinal.replace(0, str.length(), str);
- }
- }
- String[] array = arr.toArray(new String[arr.size()]);
- return array;
- }
- }
Strings
Cambio al inicializar el nextIndex
import java.text.ParseException; import java.util.ArrayList; import java.util.Arrays; import java.util.Random; import java.util.concurrent.ThreadLocalRandom; public class Kata{ public static String[] replaceLetter(String str, int n) { ArrayList<String> arr = new ArrayList<>(); StringBuilder strFinal = new StringBuilder(str); for (int i = 0; i < str.length(); i++) { int nextIndex = (i + n) % str.length(); if (str.charAt(i) != ' ' && str.charAt(nextIndex) != ' ') { nextIndex = (i + n) % str.length(); strFinal.replace(i, i + 1, String.valueOf(str.charAt(nextIndex))); arr.add(strFinal.toString()); strFinal.replace(0, str.length(), str); } } System.out.println(arr.toString()); String[] array = arr.toArray(new String[arr.size()]); return array; } }
- import java.text.ParseException;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Random;
- import java.util.concurrent.ThreadLocalRandom;
- public class Kata{
- public static String[] replaceLetter(String str, int n) {
- ArrayList<String> arr = new ArrayList<>();
- StringBuilder strFinal = new StringBuilder(str);
- for (int i = 0; i < str.length(); i++) {
int nextIndex = 0;- int nextIndex = (i + n) % str.length();
- if (str.charAt(i) != ' ' && str.charAt(nextIndex) != ' ') {
- nextIndex = (i + n) % str.length();
- strFinal.replace(i, i + 1, String.valueOf(str.charAt(nextIndex)));
- arr.add(strFinal.toString());
- strFinal.replace(0, str.length(), str);
- }
- }
- System.out.println(arr.toString());
- String[] array = arr.toArray(new String[arr.size()]);
- return array;
- }
- }
Strings
Solución espacions en blanco
import java.text.ParseException; import java.util.ArrayList; import java.util.Arrays; import java.util.Random; import java.util.concurrent.ThreadLocalRandom; public class Kata{ public static String[] replaceLetter(String str) { ArrayList<String> arr = new ArrayList<>(); StringBuilder strFinal = new StringBuilder(str); for (int i = 0; i < str.length(); i++) { int nextIndex = (i + 1) % str.length(); // check white space if (str.charAt(i) != ' ' && str.charAt(nextIndex) != ' ') { nextIndex = (i + 1) % str.length(); strFinal.replace(i, i + 1, String.valueOf(str.charAt(nextIndex))); arr.add(strFinal.toString()); strFinal.replace(0, str.length(), str); } } System.out.println(arr.toString()); String[] array = arr.toArray(new String[arr.size()]); return array; } }
- import java.text.ParseException;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Random;
- import java.util.concurrent.ThreadLocalRandom;
- public class Kata{
- public static String[] replaceLetter(String str) {
class Kata {public static String[] replaceLetter(String str) {ArrayList<String> arr = new ArrayList<>();- ArrayList<String> arr = new ArrayList<>();
- StringBuilder strFinal = new StringBuilder(str);
- for (int i = 0; i < str.length(); i++) {
- int nextIndex = (i + 1) % str.length();
if (str.charAt(i) != ' ') {//comprobar si i + 1 es un espacioint nextIndex = (i + 1) % str.length();- // check white space
- if (str.charAt(i) != ' ' && str.charAt(nextIndex) != ' ') {
- nextIndex = (i + 1) % str.length();
- strFinal.replace(i, i + 1, String.valueOf(str.charAt(nextIndex)));
- arr.add(strFinal.toString());
- strFinal.replace(0, str.length(), str);
- }
- }
// Convertimos ArrayList a ArrayString[] array = arr.toArray(new String[arr.size()]);return array;}}- System.out.println(arr.toString());
- String[] array = arr.toArray(new String[arr.size()]);
- return array;
- }
- }
Strings
Prueba de descripción
import java.text.ParseException; import java.util.ArrayList; import java.util.Arrays; import java.util.Random; import java.util.concurrent.ThreadLocalRandom; class Kata { public static String[] replaceLetter(String str) { // Hola -> oola, Hlla, Hoaa, HolH // Prueba añadir descripción ArrayList<String> arr = new ArrayList<>(); StringBuilder strFinal = new StringBuilder(str); for (int i = 0; i < str.length() - 1; i++) { if (str.charAt(i) != ' ') { strFinal.replace(i, i + 1, String.valueOf(str.charAt(i + 1))); arr.add(strFinal.toString()); strFinal.replace(0, str.length(), str); } } strFinal.setCharAt(str.length() - 1, str.charAt(0)); arr.add(strFinal.toString()); System.out.println(arr.toArray().toString()); // Convertimos ArrayList a Array String[] array = arr.toArray(new String[arr.size()]); return array; } }
- import java.text.ParseException;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Random;
- import java.util.concurrent.ThreadLocalRandom;
- class Kata {
- public static String[] replaceLetter(String str) {
- // Hola -> oola, Hlla, Hoaa, HolH
- // Prueba añadir descripción
- ArrayList<String> arr = new ArrayList<>();
- StringBuilder strFinal = new StringBuilder(str);
- for (int i = 0; i < str.length() - 1; i++) {
- if (str.charAt(i) != ' ') {
- strFinal.replace(i, i + 1, String.valueOf(str.charAt(i + 1)));
- arr.add(strFinal.toString());
- strFinal.replace(0, str.length(), str);
- }
- }
- strFinal.setCharAt(str.length() - 1, str.charAt(0));
- arr.add(strFinal.toString());
- System.out.println(arr.toArray().toString());
- // Convertimos ArrayList a Array
- String[] array = arr.toArray(new String[arr.size()]);
- return array;
- }
- }