Ad
Strings
Arrays
Code
Diff
  • 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) {
    • 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++;
    • }
    • 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
Code
Diff
  • 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) {
    • 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()]);
    • }
    • }
Strings
Strings
Code
Diff
  • 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

Controlar el indice del array. SmellCode

Code
Diff
  • 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) {
    
    			ArrayList<String> arr = new ArrayList<>();
    
    		StringBuilder strFinal = new StringBuilder(str);
    
    		for (int i = 0; i < str.length(); i++) {
    
    			if (str.charAt(i) != ' ') {
    				
    				//comprobar si i + 1 es un espacio
    	
    		        int 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 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);
    • StringBuilder strFinal = new StringBuilder(str);
    • for (int i = 0; i < str.length() - 1; i++) {
    • for (int i = 0; i < str.length(); i++) {
    • if (str.charAt(i) != ' ') {
    • if (str.charAt(i) != ' ') {
    • //comprobar si i + 1 es un espacio
    • int nextIndex = (i + 1) % str.length();
    • strFinal.replace(i, i + 1, String.valueOf(str.charAt(i + 1)));
    • strFinal.replace(i, i + 1, String.valueOf(str.charAt(nextIndex)));
    • arr.add(strFinal.toString());
    • strFinal.replace(0, str.length(), str);
    • }
    • 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()]);
    • }
    • // Convertimos ArrayList a Array
    • String[] array = arr.toArray(new String[arr.size()]);
    • return array;
    • return array;
    • }
    • }
Strings
Code
Diff
  • 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
    • //Prueba
    • 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;
    • }
    • }
Strings
Code
Diff
  • 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
          //Prueba
    			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
    • //Prueba
    • 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;
    • }
    • }